Linux Commands for DevOps
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 :
echo - prints the specified message to the screen
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
cd - change directory
cd .. - move one directory back
cd ../.. - move two directories back
pwd - displays present working directory
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
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
For multiple commands use a semicolon(;)
man - to know the entire information of any command (manual)
clear - clears the terminal
whoami - to check who the current user is
su - switch user
su root - to switch to root user
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.
touch - used to create a file
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
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
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.
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.
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
free : to check memory of the server
nproc : to check the number of CPUs available or running
df : to check disk size of the server
top : to check the node status i.e., everything about the server. To display memory, CPU and disk size at a time.
ps -ef : displays all the processes running on the server.
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
find - to find any file from the entire file system
find / -name filename
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
curl - fetches information from the internet
for example, curl URL
wget - fetches and downloads the information from the internet onto the server
set -x : sets the shell script in debug mode. The script is executed command by command displaying the command along with it's output.
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.
set -o pipefail : exits the script execution for a pipefail also.
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.
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.
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
chown - change ownership for a file or directory
chown new_owner_unsername old_owner_username
chmod - used to change the access permissions of files and directories
example : chmod 777 filename.txt
history - displays all the commands used so far.
lsof - list open files
lsof -u username : list the open files opened by a particular user
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
tar - to zip and unzip files of .tar format
tar -cvf filename source_foldername : to zip a folder
tar -xvf filename : unzip a file
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.
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.
uniq - filters out duplicate lines in files
uniq filename
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.
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