Other Parts of This Series:
DevOps Linux File Manipulation Commands (Photo Credit: Unsplash)
Story:
Rasel grabbed the knowledge and understanding about the Linux kernel, how it boots, and the Linux file system. As the server runs mostly using CLI and no UI like Windows, so Rasel now wanted to learn the commands that help to work with files and directories. Like commands that are used for file and directory creation and content in its manipulation.
Basic Structure of a Command:
The general structure of a Linux command line looks like:
| |
- command - is an executable file or program to be run/execute. Like: cd, ls, cat.
- flag - A short switch (usually with -) that modifies the command’s behavior. Like: -l.
- option - Like a flag, but often with a value or longer form. Like: –sort=time.
- argument - The target on which the command operates. Like: a file or directory name.
Helper Commands:
Linux has some helper commands which helps to find and understand other command, in case need clarifications or clues. Some of the most common of them are:
| |
This gives description and prototype of a command.
| |
This gives manual of a command.
| |
This gives single line description of a command.
| |
This gives possible command with the given keyword.
Example:
Directory Related Commands:
Linux has some basic commands which helps to create, rename, delete and move directories. Means command that help to work with directory. Some of them are:
| |
This create a new directory with the given name.
| |
This checkout into the given directory name.
| |
This print or give current working directory.
| |
This remove or delete the directory with given name.
| |
This move source directory to the given destination directory. Interestingly this command is also used for rename as well.
Example:
File Related Commands:
Linux has commands which helps to create, rename, delete and move files. Means command that help to work with file. Some of them are:
| |
This command gives the file type of the given file.
| |
This command create a new file with the given name.
| |
This command remove or delete the file with given name.
| |
This command move the file source file to the destination. This also used for rename as well.
Example:
File and Directory Structure Explore and Visit Commands:
Linux has commands which helps to explore the file and directory structures. Also help to locate and jump from one to another location. Some of them are:
| |
This command gives the tree of the file and directory relationship.
| |
This command gives the file and directory list.
| |
This command checkout into the given directory name.
Example:
File Content Related Commands:
Linux has commands which helps to work with file content. Means manipulating a file. Some of them are:
| |
This command view the full content of file with the given name.
| |
This command view the first 10 lines content of file with the given name.
| |
This command view the last 10 lines content of file with the given name.
| |
This command start inserting for content to file with the given name.
| |
This command copy the file source file to the destination.
| |
This command find files with the given regex.
| |
This command find content with the given regex within given file name.
| |
This command replace content before with after of given file name.
Other command as well like, sort for sorting content, uniq for getting unique content, cut for getting split column content etc.
Example:
Working With Vim:
Vim is an advanced text editor based on the older vi. It’s keyboard-driven, super fast, and works inside the terminal, perfect for server environments.
Vim has 3 main mode:
- Normal Mode (Default): Navigate and issue commands (Esc key)
- Insert Mode: Insert text (like typing normally) (Press i, I, a etc.)
- Command Mode: Save, quit, search, etc. (Press : from Normal)
| |
This command open the file for working with the given name.
Vim Summary:
- Open file - vim filename
- Insert mode - i, a, o, O
- Save + Quit - :wq or ZZ
- Quit without saving - :q!
- Search - /pattern then n / N
- Replace all - :%s/old/new/g
- Copy/Cut/Paste - yy, dd, p
- Undo / Redo - u, Ctrl + r
- Go to line - :line_number or G / gg
Summary:
In Linux, working with files and directories is an essential skill for DevOps. You can create files using touch and directories using mkdir. To view or organize them, you use commands like ls, cp to copy, mv to move or rename, and rm to delete. To view file content, you use cat, less, head, and tail, while tools like vim help you edit files directly from the terminal. Searching inside files can be done with grep, and locating files can be done with find. For text manipulation, you use commands like cut, sed, sort, uniq to extract, modify, or organize text data.