Linux Commands for DevOps

·

6 min read

Linux is an open-source and community-developed operating system that is capable of handling activities from multiple users at the same time. It is one of the most widely used Operating Systems.

Linux is -

  • free

  • open source

  • highly secure

  • high stability and performance

Linux does not need to be rebooted after a short period, it rarely slows down or freezes, and it provides high performance on various networks and workstations, it allows a large number of users to work simultaneously and handle them efficiently.

Hence, Linux is preferred over other operating systems.

Some of the most useful Linux commands for DevOps :

  1. echo - prints the specified message to the screen

  2. ls - lists files and folders

    flags :

    -l : to view owners and their permissions

    -t : to view timestamps (i.e., the time of creation of file or directory)

    -r : to list files in reverse order

  3. cd - change directory

    cd .. - move one directory back

    cd ../.. - move two directories back

  4. pwd - displays present working directory

  5. mkdir - used to create a new directory (make directory)

    example : mkdir /home/dir1

    mkdir -p : creates parent directory and a subdirectory ( directory hierarchy )

    example : mkdir -p first/second/third

  6. rmdir - remove directory

    By default, it can remove only an empty directory.

    flags :

    -p : removes both parent and child directory

    -pv : removes parents, subdirectories and verbos

  7. For multiple commands use a semicolon(;)

  8. man - to know the entire information of any command (manual)

  9. clear - clears the terminal

  10. whoami - to check who the current user is

  11. su - switch user

    su root - to switch to root user

  12. sudo - used as prefix for commands for user who does not have privileges for executing some commands

    sudo useradd username - add a user

    sudo passwd username - set password for the user created

    sudo userdel username - delete a user

    sudo groupadd groupname - add a group to the system. A group has multiple users.

  13. touch - used to create a file

  14. vi - used to open a file if file exists else create and open the file.

    i : enter into insert mode to write into a file

    esc :w - save

    esc :wq - save and exit

    esc :q - exit

  15. cat - Displays the contents of a file. Also to read, modify or concatenate files.

    flags -

    -b : add line numbers to the non blank lines of the file

    -n : add line numbers to all the lines

    -s : squeeze all blank lines into one line.

    -E : adds the '$' sign at the end of each line

  16. cp - to create a copy of the contents of a file or directory

    flags -

    -n : does not overwrite the file

    -u : updates the destination file only when the source file is different from destination file

    -R : recursive copy of all files and directories in source directory tree. It also copies the hidden files.

  17. mv - to move files from one directory to another

    mv filename destination_path

    flags -

    -u : updates the file or folder

    -v : enters into the verbos mode. It prints the source and destination file.

    Verbos mode : it provides additional details as to what drivers and software it loads during start up.

  18. rm - removes files from a directory

    rm filename

    rm -r : removes directory

    rm * : all the files will be deleted

    rm -rp : removes parents and sub directories of a directory

  19. free : to check memory of the server

  20. nproc : to check the number of CPUs available or running

  21. df : to check disk size of the server

  22. top : to check the node status i.e., everything about the server. To display memory, CPU and disk size at a time.

  23. ps -ef : displays all the processes running on the server.

  24. grep - used to search for a particular string or word in a text file.

    grep word filename

    flags :

    -i : for case insensitive strings

    -n : returns matching strings along with the line numbers

    -v : returns the lines not matching the string

    -c : returns the number of lines in which the string matches

  25. find - to find any file from the entire file system

    find / -name filename

  26. trap - traps signals. Trapping signals allows the execution of the script which otherwise would've been stopped by the interrupts.

    trap "echo 'Don't use ctrl + c'" SIGINT

  27. curl - fetches information from the internet

    for example, curl URL

  28. wget - fetches and downloads the information from the internet onto the server

  29. set -x : sets the shell script in debug mode. The script is executed command by command displaying the command along with it's output.

  30. set -e : exits the script execution when there is an error. Without this command, the script prints the error when occurred and continues executing the rest of the script.

  31. set -o pipefail : exits the script execution for a pipefail also.

  32. pipe statement ( | ) : this parameter send the output of the first command to the second command. This allows filtration of the output.

    for example, ps -ef | grep amazon : This displays all the processes having the string amazon.

  33. awk - filters out information from the output passed. It prints specifically a particular column from all the rows of the output.

    for example, ps -ef | grep amazon | aws -F " " ' { print $2 } ' - this prints the second column from the all rows of processes having the string amazon.

  34. sort - sorts the result of a search either alphabetically or numerically. Files, file contents and directories can be sorted.

    sort filename - sorts file contents

    flags :

    -r : returns result in reverse order

    -f : case insensitive sorting

    -n : returns result in numerical order

  35. chown - change ownership for a file or directory

    chown new_owner_unsername old_owner_username

  36. chmod - used to change the access permissions of files and directories

    example : chmod 777 filename.txt

  37. history - displays all the commands used so far.

  38. lsof - list open files

    lsof -u username : list the open files opened by a particular user

  39. id - to find the user, group name, ids of users on server

    id username

    flags :

    -g : only effective group ids

    -G : all group ids

    -n : names

    -r : real id

    -u : user id

    -help : help messages

    -version : version information

  40. tar - to zip and unzip files of .tar format

    tar -cvf filename source_foldername : to zip a folder

    tar -xvf filename : unzip a file

  41. cut - for extracting a portion of a file

    cut -c1 filename : first column

    cut -c1-2 filename : -c stand for column , 1-2 means column 1 and 2. Displays content of column 1 and 2.

  42. sed - text editor that performs editing operations in a non-interactive way. We can do insertion, deletion, search and replacement. It is used to perform complex pattern matching.

    for example, sed 's/word1/word2/' filename : replacement of word1 with word2. s means substitution.

  43. uniq - filters out duplicate lines in files

    uniq filename

  44. watch - executes a program periodically showing output. Runs a specified command in the argument repeatedly by showing its outputs and errors. It runs until interupted.

    watch -flag command

    flag :

    -d : highlights differences between successive updates.

  45. eval - executes arguments as a shell command. A command can be stored in a variable and to execute the command stored in it, we use eval.

    $a = "ls"

    $ eval $a