Photo by hannah joshua on Unsplash
Essential Linux Commands for DevOps: Part 1
Mastering Linux Commands for Efficient DevOps Workflow
Essential Linux commands for DevOps include cat, touch, Vi editor, Nano editor, mkdir, viewing files and directories, copy, move and rename, and remove. These commands allow for file manipulation, text editing, directory management, file viewing, copying, moving, renaming, and removal.
cat
To create a file -> cat > <file-name> (It will then ask us to type. After typing press ctrl + d. Here the '>' symbol indicates whatever input we give put that input in the specified file.)
To view the contents of the file -> cat <file-name>
To concatenate multiple files into a single file -> cat <file1> <file2> > file3 (We can concatenate multiple files at once.)
To append into an existing file -> cat >> <file-name>
To see the content of the file in reverse order -> tac <file-name>
touch
To create a file with a file name -> touch <file-name>
To create multiple files at once -> touch <file-name1> <file-name2>
To change the timestamp of a file -> touch <file-name> (It will change the access time, modify time and change time. To view the timestamp of the file use the command stat <file-name>.
To change only access time -> touch -a <file-name>
To change only modify time -> touch -m <file-name>
Vi editor
It is known as vim editor. This is a default editor that comes with Linux. Using this editor we can create and view the files. It is mostly used for programming. This can be used to view an existing file or create a new one.
To start with vi editor follow these steps:-
Type vi <file-name> (This will open the file in the editor)(Example - vi hello.txt)
Whenever we execute the above command we are in read-only mode. To enter into insert mode i.e; to change something press 'i' on the keyboard.
When you are done with your work press ':' on the keyboard followed by any of the below characters:
w -> To only save the file. (This will not close the editor)
wq / x -> This will save the file and close the editor
q -> This will close the editor
q! -> Let's say you have made some changes to the file and now you do not want those changes this command will forcibly close the editor without saving the changes.
Nano editor
Like vim it is also a text editor. It has an easy-to-use interface. We can directly make changes in the file unlike in vim in which we have to enter insert mode.
If you have not made any changes in the file press ctrl + x to exit the editor.
If you made changes in the file press ctrl + o to override the previous changes it will then ask for a name, change the name and press enter. After that press ctrl + x to exit the editor.
mkdir
To create directories(folder) -> mkdir <directory-name>
To create directories within directories -> mkdir -p dir1/dir2/dir3
To create multiple directories -> mkdir dir1 dir2 dir3
To change the current directory -> cd <directory-name you want to move into>
To go back into the previous directory -> cd ..
To see the current directory path -> pwd
To go back into 3 directories back -> cd ../../.. (The number of directories we want to go back to that many '..' we will include
View files and directories
To view all the files and directories in the current directory -> ls (ls -l to view details)
This includes all the hidden files and directories -> ls -a (ls -al to view details)
To create a hidden file -> touch .<file-name>
To create a hidden directory -> mkdir .<dir-name>
To view the first few lines of the file -> head <file-name> (By default it shows the first 10 lines.)
To view the first 3 or n lines of the file -> head -3 <file-name>
To view the last few lines of the file -> tail <file-name> (By default it shows the last 10 lines.)
To view the last 3 or n lines of the file -> tail -3 <file-name>
To view the contents of the file page by page -> more <file-name>
Press enter to scroll down line by line.
Space bar to go to next page.
Press b on the keyboard to go back to the previous page. (Page - How much content can fit in the current Linux terminal window size.)
To view the contents of the file one page at a time -> less <file-name> (In the case of large files it is faster because it doesn't load the entire file in the memory instead it loads part by part.)
Copy
To copy the contents of one file to another -> cp <source-file> <dest-file> (This will copy the content of the source file and override it in the destination file.)
To copy a file into some directory -> cp <file-name> <directory-name>
To copy a directory into another directory -> cp -r <dir1> <dir2> (Here -r means recursively copying every subfolder and file.)
Move and rename
To move a file into another directory -> mv <file-name> <directory-name>
To rename a file -> mv <old-name> <new-name>
(mv <old-name> .<new-name> This command will change the name but will make the file hidden.)
Remove
To remove empty directories -> rmdir <directory-name> (Directory should be empty.)
To remove both empty and nonempty files and directories
-> rm -rf <file-name>/<directory-name>
(f - forcibly, r - recursively)
To remove everything in the current directory -> rm -rf *
Conclusion
In conclusion, this article provides a comprehensive overview of essential Linux commands for DevOps, covering file manipulation, text editors, directory management, file viewing, copying, moving, renaming, and removal. Mastering these commands will greatly enhance your efficiency and productivity when working with Linux systems in a DevOps environment.