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

  1. 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.)

  2. To view the contents of the file -> cat <file-name>

  3. To concatenate multiple files into a single file -> cat <file1> <file2> > file3 (We can concatenate multiple files at once.)

  4. To append into an existing file -> cat >> <file-name>

  5. To see the content of the file in reverse order -> tac <file-name>

touch

  1. To create a file with a file name -> touch <file-name>

  2. To create multiple files at once -> touch <file-name1> <file-name2>

  3. 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>.

  4. To change only access time -> touch -a <file-name>

  5. 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:-

  1. Type vi <file-name> (This will open the file in the editor)(Example - vi hello.txt)

  2. 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.

  3. 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

  1. To create directories(folder) -> mkdir <directory-name>

  2. To create directories within directories -> mkdir -p dir1/dir2/dir3

  3. To create multiple directories -> mkdir dir1 dir2 dir3

  4. To change the current directory -> cd <directory-name you want to move into>

  5. To go back into the previous directory -> cd ..

  6. To see the current directory path -> pwd

  1. 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

  1. To view all the files and directories in the current directory -> ls (ls -l to view details)

  2. This includes all the hidden files and directories -> ls -a (ls -al to view details)

  3. To create a hidden file -> touch .<file-name>

  4. To create a hidden directory -> mkdir .<dir-name>

  5. To view the first few lines of the file -> head <file-name> (By default it shows the first 10 lines.)

  6. To view the first 3 or n lines of the file -> head -3 <file-name>

  7. To view the last few lines of the file -> tail <file-name> (By default it shows the last 10 lines.)

  8. To view the last 3 or n lines of the file -> tail -3 <file-name>

  9. 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.)

  10. 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

  1. 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.)

  2. To copy a file into some directory -> cp <file-name> <directory-name>

  3. To copy a directory into another directory -> cp -r <dir1> <dir2> (Here -r means recursively copying every subfolder and file.)

Move and rename

  1. To move a file into another directory -> mv <file-name> <directory-name>

  2. 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

  1. To remove empty directories -> rmdir <directory-name> (Directory should be empty.)

  2. To remove both empty and nonempty files and directories

    -> rm -rf <file-name>/<directory-name>

    (f - forcibly, r - recursively)

  3. 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.