Linux cheat sheet 🐧

An operating system is responsible for directly managing a system’s hardware resources like CPU, memory, and storage. Linux is one of the most widely-used open-source operating systems. It is a bundle of tools, programs, and services along with the Linux kernel which provides a fully functional operating system. Linux can be differentiated from other operating systems in many ways. Firstly, Linux is open-source software. Different Linux distributions come with different software options. Each distribution has its own approach to desktop interfaces. As such, users have the ability to choose core components such as graphics, user-interface, etc., as per their preference.

Here, we have attempted to cover the widely used Linux commands in the Linux Command Cheat Sheet. With new Linux distributions, there are chances of a few commands getting deprecated. So, make sure you keep up with the new releases, their new additions, and commands which are rendered deprecated.

Things to know:
· "ELF" file: The native executable file format used in Linux systems
· ltrace is used to trace library calls made by a program, which essentially logs user API calls.
· strace is used to trace system calls, which are the API calls directed towards the kernel.
· C is king for OS development due to its efficient hardware interaction and the ability to predict the assembly language output of C code.


How to diagnose a mysterious process that’s taking too much CPU, memory, IO, etc?

The diagram below illustrates helpful tools in a Linux system.

🔹‘vmstat’ - reports information about processes, memory, paging, block IO, traps, and CPU activity.
🔹‘iostat’ - reports CPU and input/output statistics of the system.
🔹‘netstat’ - displays statistical data related to IP, TCP, UDP, and ICMP protocols.
🔹‘lsof’ - lists open files of the current system.
🔹‘pidstat’ - monitors the utilization of system resources by all or specified processes, including CPU, memory, device IO, task switching, threads, etc.

linux performance observability


Linux Performance Tools


ApplicationDescriptionCategoryCategory
alias Lists all your current aliases It allows one command to be substituted by another. alias home=’cd home/hackr/public_html’ Shell Commands
set -x It is a built-in function that is used to define the values of the system environment. set [--aefhkntuvx[argument]], where a, e, f, h, k, n, t, u, v, x are the different options that can be used with this command. Shell Commands
\ The backslash character is used to override any aliases.   Shell Commands
script It automatically creates a typescript or a capture-log of a session and writes it to a file. It includes all commands and their outputs as well. script [options] [file] Shell Commands
~ The tilde character is used as an alias to a user’s home directory.    Shell Commands
reset It reinitializes your current terminal. This is generally useful when a program dies leaving the terminal in an abnormal state. reset [terminal] There are a number of options that can be used with reset to customize the output. Shell Commands
exit Closes your current terminal or logs out. Also, CTRL+D can be used. exit Shell Commands
logout Logs out of the current terminal. Also, CTRL+D can be used. logout Shell Commands
echo Repeats anything that is typed after it. echo “text” echo rm -R Shell Commands
history It is used to list the log of typed commands.  history n – Lists the last n number of commands  !<command_name> will list the last command typed with <command_name> Shell Commands
CTRL + D It is an end-of-file key combination and can be used to log out of any terminal.   Combination Keys
CTRL + Z This key combination is used to stop a process. Also, it can be used to run a background process.   Combination Keys
CTRL + A | CTRL + E These key combinations are used to navigate to the start and the end of a line on the command line.   Combination Keys
CTRL + K Used to delete what is in the front of the cursor.   Combination Keys
CTRL + Y Pastes the last text that was deleted.   Combination Keys
CTRL + W Cuts or deletes the entire line that has been typed.   Combination Keys
Screen This command is used to switch between multiple virtual terminals. It is mainly useful with remote login. By default, you start with one terminal. CTRL + A & C can be used to create a new virtual terminal.   Combination Keys
CTRL + N CTRL + N is used to go to the next virtual terminal   Combination Keys
CTRL + P CTRL + P is used to go to the previous virtual terminal.   Combination Keys
CTRL + C This combination key breaks/kills a process that is running on the terminal.   Combination Keys
CTRL + ALT + DEL This key combination can be used from a terminal to reboot or shutdown. Here the user does not have to be logged in.   Combination Keys
man Displays the summary of a program from an online manual. man <program_name> man -f: Lists the details associated with the command. man -K <keyword> : searches the manual for the specified keyword. Help
info Gives a more detailed hyper-text manual on a particular command. info <program_name> Help
whatis Describes a program in one-line. whatis <program_name> Help
apropos It searches the database for strings. apropos <string> Help
> Send information to a file. cat file1 file2 > file1_and2.txt Input/Output
< Inserts information into a text file. It is generally used with other commands that are used to extract information through standard input. Ex: tr ‘[A-Z]’ ‘[a-z]’ <filename.txt >newfilename.txt  Input/Output
>> Appends information at the end of file. It creates the file if it does not exist.   Input/Output
<< It is mainly used in shell scripting. It is also used with command that are used to extract information.    Input/Output
2> Used for redirecting error output.   Input/Output
| It sends the output of one command as an input for another command.   Input/Output
Tee Sends the output of a file to the standard output. tee -options file_name.txt Input/Output
&> Used to redirect output to a specific location make &> /dev/null Input/Output
` ` Used to direct the output of the second command to the first command. 1st_command `2nd_command -options` Command Substitution
$ Used to direct the output of the second command to the first command. 1st_command $(2nd Command) Command Substitution
| Used to direct the output of the second command to the first command. cat command1 | command2 Command Substitution
&& Runs the 2nd command only if the 1st command runs successfully. 1stcommand && 2nd Command Multiple Commands
|| Runs the 2nd command only if the 2nd command does not run successfully. 1st Command || 2nd Command Multiple Commands
; 2nd Command is executed after the 1st Command is executed. 1st Command; 2nd Command Multiple Commands
access This command is used to check if a program has access to a specified file or whether the file exists or not. access (const char *path, int mode) File Management & Handling
awk This command is a scripting language used for manipulating data and generating reports. awk -options filename.txt File Management & Handling
batch This command is used to read commands from the standard input or a file and execute them when permitted by system load levels. batch File Management & Handling
bc Used for command line calculator bc -options file_name.txt File Management & Handling
cd Change Directory. Used to go to the previous directory. Can be used with the absolute path or the relative path. cd  cd pathname File Management & Handling
ls Lists files and directories. Does not list hidden files or directories ls -options, where options can be l, a, d, F, S, R. File Management & Handling
pwd Print the current working directory pwd File Management & Handling
tree Lists all files and directory recursively. tree File Management & Handling
find This is used to search for files on a filesystem. The search can be customized with many options. find / -name file  File Management & Handling
slocate Lists all the files in the system that match the specified pattern slocate string File Management & Handling
whereis It locates the source, binary and the manual page for a particular program. whereis program_name File Management & Handling
which It only looks for the executable program which program_name File Management & Handling
mkdir Make Directory.  mkdir Dir1 mkdir -p /home/dir1/dir2/dir3 This command creates dir1, dir2 and dir3 as it goes. File Management & Handling
rm Removes or delete a file or a directory rm -options file_or_folder File Management & Handling
rmdir Removes an empty directory. rmdir directory File Management & Handling
mv Used to move a file or directory to another location. It can also be used to rename a file or a directory. mv existing_file/folder new_location File Management & Handling
cp Copy a file  cp -options file_to_be_copied new_location File Management & Handling
ln Used to create a link to a file. Links can be hard-links or symbolic links. A hard-link is a reference to a file while a symbolic-link is similar to a shortcut link. ln target_name link_name ln -s target_name link_name File Management & Handling
shred Overwrites a file and removes it in order to make it irretrievable by the software or hardware. shred -n 2 -z -v /dev/hda1 File Management & Handling
du It is used to display information about the file size. It can also be used on directories, which then displays the information about all the size of all the files and subdirectories. du -options file_name/directory_name File Management & Handling
file It shows the type of a particular file. It displays the inode number, creation data/access date along with some advanced options. file file_name File Management & Handling
stat Give detailed information about a file. stat file File Management & Handling
dd Used to create copies of disks, perform conversions on files and very the block size while writing the file. dd operand dd option dd inputfile_path outputfile_path File Management & Handling
touch Used to create empty files.  It is also used to increase the timestamps on the file.  touch -t 09072019 filename.txt File Management & Handling
split It is used to split files into several small files.  split -options filename split -b xx – splits the file into xx bytes, split -k xx – splits the file into xx kilobytes split -m xx – splits the file into xx megabytes File Management & Handling
rename It is used to rename files as per regular expression. rename -options file_name File Management & Handling
time It is a utility that measures the time taken by a program to execute.  time program_name options System Information
/proc The files under /proc displays system information.  cat /proc/cpuinfo – displays information about the CPU. System Information
dmseg This command is used to print the contents of the bootup messages displayed by the kernel. This is particularly useful in debugging issues. dmseg System Information
df Displays the information about the space on the mounted file-systems. df -options /dev/hdx System Information
who Displays information about the logged in users including their login time. who System Information
w This command displays who are logged in into the system and the processes they are running.  w System Information
users This command prints the name of the currently logged in users. users System Information
last This command displays the time of the logged-out users. This also displays the information when the computer was rebooted. last System Information
lastlog Displays a list of the users and the time/day of their login. lastlog System Information
whoami It tells the username of the logged in user. whoami – it does not need or take any options. System Information
free Displays memory status. (Total, Used, Free, cached, Swap) free -tm, where t displays the total statistics and m displays the space in megabytes. System Information
uptime | w It displays how long the computer has been up and running. Additionally, it displays the number of users and the processor load.   System Information
uname It is used to display system information such as OS type, kernel version, etc. uname -options, where the options can be – a, n, m, s, r, p System Information
xargs This command is used to run a command as many times as required.  Ex: ls | xargs grep “search string” The various options that go with it are:
  • Nx, lx, p, t, i.
System Information
date It is used to display the system. It can also be used to set the date/time. date -s hh:mm:ss System Information
cal This command displays the calendar of the current month. cal -y – Displays the calendar of the specific year. System Information
acpi This command is used to display the battery status and other ACPI(Advanced Configuration and Power Information) related  information. acpi -options Using different options will give different system information. System Information
acpi_available This command is used to test if the ACPI subsystem is available. acpi-available System Information
aptitude  This command opens up a highly built-in interface to interact with the package manager of the machine.   System Information
proc This command gives information about the hard- drive. cd /proc/ide0/had System Management
fdisk It gives information on any hard drives that is connected to the system and their partitions.  fdisk  System Management
shutdown now It starts the process of shut-down immediately. This command can also mean go to a single user mode. This command can also be used to shutdown a system at a particular time. shutdown now System Management
halt This command shuts down the system immediately. same as shutdown -h System Management
reboot This command is used to reboot the computer immediately. same as shutdown -r System Management
ps This displays the list of processes running on the system. When used without any options, it lists the processes run by the current user in the controlling terminal. ps -aux: lists all processes run by all users. System Management
pstree Lists all the processes in a tree structure. pstree -p: lists all processes along with their ID’s System Management
pgrep This command is used for finding processes via keyword terms. Generally, this command returns only the PID no. It can be used to list process names or via usernames.  pgrep System Management
top This command is used to list process in the order of CPU usage. It has many additional options that go with it, which helps to get the desired result. top System Management
kill This command is used to kill a process. It requires the pid or the id of the process. kill pid kill %id System Management
killall This command is used to kill a process by its name.  killall -v: will make the process report if the kill was successful. killall -i – will prompt the user to confirm before attempting the kill. System Management
pkill This command is used to kill a process specified by a regular expression.  pkill process_name pkill -u: is used to kill the processes of a certain user. System Management
skill It is used to continue, kill, stop the process. The process can be specified via username, command name or process-id. Ex: skill -stop unauthorised_username System Management
jobs It displays the list of currently running jobs in order of execution. This command is available only in bash, csh, ksh and tcsh shells. jobs [job_name] System Management
bg It runs a process in the background. An & sign at the end of the command can also be used to run a job at the background.  bg job_number bg  job_name System Management
fg This command is used to bring a process at the foreground. This will take over your current terminal.  fg job_number fg job_id System Management
nice This command is used to set the CPU priority for a process. In order to set a higher priority for a process, the user needs to be a root user. However, any user can lower the priority of a process.  nice -20 <process_name>: sets the maximum priority nice 20 <process_name>:: sets the minimum priority System Management
renice Changes the priority of an existing command.   System Management
snice Along with changing the priorities of a process, snice can also be used to stop, continue or kill a process. snice -upctvi, where u,p,c,t,v,I are all options that make snice perform differently. System Management
service It allows to perform different tasks on services. service -sfR, where s, f, R are all different options that can be used with it.  System Management
autoconf This command is used in Linux to generate configuration scripts.   System Management
su username This command is used to switch to a different username. In order to return to the original username, you need to type exit or use CTRL+D. su username User Management
root This is the superuser. The overall power of the system lies with this user. This includes all administrative powers as well.    User Management
chsh This command is used to change the login shell.  chsh [enter] <shell_name> to be used every time you login. User Management
chfn This command is used to change the finger information. This change is reflected in the /ect/passwd file chfn user_name User Management
passwd This command can only be executed by the root user. It allows the user to change the password of a user. passwd username User Management
vi It is a very powerful text editor which is known for its minimal use of resources.   Text Command
emacs It is an advanced text editor which enables a user to browse the internet, play games, chat, etc.   Text Command
head When used without any options, it shows the first 10 lines of a file. head -n x, where x specifies the number of lines to be displayed from the top. Text Command
tail When used without any options, it is used to show the last 10 lines of a file. tail -n x, where x specifies the number of lines to be displayed from the last. Text Command
less This command enables to view text and scroll backwards and forwards. less <file-name> Text Command
more This command enables to view one page at a time.   Text Command
cat Used to concatenate/combine multiple files into one.  cat <file_1> <file_2> > <new_file> Text Command
tac Combines multiple files into one, however the files will be combines in reverse order. tac <file_1> <file_2> > <new_file> Text Command
z* | bz* commands Commands can be prefixed with a z in order to allow them to work within a gzip compressed file.   Text Command
wc This returns the word count in a text file.  wc -wlc <filename>, where w returns the number of words, l returns the number of lines and c returns the number fo characters. Text Command
style This command allows to run various readability tests on a file.  style -options <file_name> Text Command
cmp This command is used to determine the differences between two files, at the binary level. cmp -option filename1.txt filename2.txt Text Command
diff This command outputs the difference in text between two files.  diff <flie_name1> <file_name2> Text Command
sdiff This command displays the file contents in two columns. sdiff <flie_name1> <file_name2> Text Command
diff3 This command outputs the difference in text between three files.  diff <flie_name1> <file_name2> <file_name3> Text Command
comm This command enables us to compare two files and displays the unique contents of file1, unique contents of file2 and those common to both in three separate columns. comm <file1> <file2> Options -1, -2, -3 can be used to suppress the display of the corresponding column. Text Command
look This command enables you to display a list of words from the system dictionary that begins with a specified string. look <string> Text Command
sort This command is used to sort text in a file. When used without any options, it sorts the text alphabetically. It is a powerful command and can be used with many options to achieve the required output. cat result_list.txt | sort Text Command
join This command will allow joining of two lines under the consideration that both the lines have a specific value in common. join <file_1> <file_2> Text Command
cut This command helps to remove certain sections of a line. This can be done based on tabs, commas, etc. cut -options <file_name> Text Command
ispell/aspell This command enables a user to find detect misspelt words and replace the word or continue. ispell <file_name.txt> aspell -c <file_name.txt > Text Command
chcase/tr This command is used to change the uppercase letters in a file to lowercase. cat <original_filename.txt> | chcase ‘[A-Z]’ ‘[a-z]’ > newfile.txt Text Command
fmt It is a simple command for text formatting.    Text Command
paste This command can be used to put lines from two files together. It arranges the lines as per options used. paste <file_name1> <file_name2> Text Command
expand This command will convert 2 tabs into spaces and output it. expand <file_name.txt> Text Command
unexpand This command will convert spaces into tabs and display it, unexpand <file_name.txt> Text Command
uniq This command is used to remove the duplicate entries from a file. uniq <list.txt> Text Command
nl This command is used to add numbers to each line and indent the text. nl <file_name.txt> Text Command
perl This command is used to search and replace text.  perl -pi -e “s/search_string/new-string/g;” <file_name.txt>  Text Command
dos2unix/ fromdos This command is used to convert Microsoft style end-of-line characters to Unix System end-of-line characters. dos2unix <file_name.txt> Text Command
unix2dos/todos This command is used to convert Unix System style end-of-line characters to Microsoft System end-of-line characters. unix2dos file_name.txt Text Command
antiword This is a filter command that is used to convert Microsoft word documents into plain ASCII text documents. antiword file_name.txt Text Command
recode This command is used to convert text files between various forms of text encodings. By default, recode overwrites the file, hence it is suggested the user to use necessary options to avoid losing the original content. recode ..pc < file_name.txt > new_file.txt Text Command
enscript This command is used to convert text files to postscript, HTML, rich text file, etc. It goes with a number of options that helps to customize its output. enscript - -language=HTML original_file.txt -o output_file.txt Text Command
figlet This is a unique command that can be used to convert an ASCII art.    Text Command
grep This command is used to search for a text within files.  grep ‘search_word’ file_name.txt Options like v,n,w,A x, B x and/or r can be used to customize the output. Text Command
rgrep It is recursive grep. It searches the respective subdirectories and files in the directory and displays the files names and the matching line. grep -r option can be used for the same effect. Text Command
fgrep This command is used to find string literals only. fgrep ‘string literal’ file_name.txt Text Command
units It helps to convert units of measurement between different scales. units – verbose To exit – CTRL+D  Mathematical Commands
python It is a programming language that also makes a good calculator. This command executes the Python interpreter in interactive mode. python To exit: CTRL – D Mathematical Commands
numgrep It searches for different occurrences of numbers, basically ranges, factors and sequences of numbers.  numgrep /<expression>/ Mathematical Commands
ifconfig This command is used to configure network parameters. ifconfig -options Network Commands
tcpdump It is a program that sniffs packets off network interface and interprets them. sudo tcpdump -I any : captures all packets in any interface. Network Commands
ping It sends echo requests to the specified host and lists the responses received in their round-trip time. ping ip_or_host_name To stop ping – CTRL + C Network Commands
hostname Displays the hostname of the    Network Commands
traceroute Shows the route of a packet. traceroute machine_name or IP Network Commands
tracepath It si similar to traceroute, however it does not take complicated options with it. tracepath machine_name or IP Network Commands
findsmb It lists information about machines that respond to SMB name queries findsmb Network Commands
telnet This command helps to establish connection between two hosts. telnet [hostname] Network Commands
nmap It is used to query machines, if they are up and which ports they are on. nmap machine_name Network Commands
ip  The ip command is used to perform network administration tasks. It can be used to set or delete ip addresses, verify changes, show routing table, change the default route, show network statistics, monitor netlink messages, activate and deactivate network interface and more. ip a (ip address) ip (link) ip -s(ip stats) ip tunnel ip r Network Commands
host Looks up the internet address using the Domain Name Server. host_ipaddress | host domain_name Network Commands
dig It is the domain information groper tool which outputs IP address, hostname and various other information. dig -x <ip-address> returns the hostname dig works with various other options. Network Commands
whois Used to look up contact information    Network Commands
wget Used for downloading files from the world wide web wget file_url: gets a file from the url wget -m: archives a single website wget -nc: to avoid overwriting and already archived file. wget -c: to continue archiving a file that was previously unfinished.  Network Commands
curl It is used to download information remotely. It works without user interaction and supports many internet protocols. It can access dictionary servers, ftp, http, gopher,etc.  curl -M: this will take you to its manual page. It has a lot many options going with it for accomplishing various tasks. Network Commands
ssh It is used to securely connect to a remote server/system. It transfers data in an encrypted from between the host and the client. ssh user_name@host(IP/domain_name) Remote Administration
scp Allows copying of one file from one computer to another. scp machine-to-be-copied-from machine-to-be-copied-to scp -r: Used for recursive copying. Remote Administration
sftp Secure ftp. It is an encrypted version of the ftp command, which transfers securely over a network. sftp [user@]host[:file…] sftp [user@]host[:dir[/]] sftp -b batchfile [user@]host There are many other options that goes with this command. Remote Administration
md5sum Computes an md5 checksum to for the specified file_name to verify its integrity. md5sum file_name Security
mkpassword -1 10 Gives a random password of 10 characters   Security
at ‘at’ is used to execute a command once on a specified day, at a specified time. at 22:00 Then type the command and press the end-of-file key. Background Commands
at now+time Runs the command at the + the time time specified.  at -f shell_script  now+1hr Background Commands
atq Lists the job in the queue. It does not take any options.   Background Commands
atrm Removes a job from the at queue. atrm job_no Background Commands
cron It is used to schedule a particular function every minute, hour, day, week or month. anacron is a command used for machines which are switched off. It runs the command when the machine is next switched on. Background Commands
crontab It edits, reads and removes the files read by the cron daemon.  -e – edits file -l – lists the contents of the file -u username – to works with other uses crontab file. Background Commands
@reboot Runs the command at reboot   Shortcut Commands
@yearly | @annually Same as 0011*   Shortcut Commands
@monthly Same as 001**   Shortcut Commands
@weekly Same as 00**0   Shortcut Commands
@daily | @midnight Same as 00***   Shortcut Commands
@hourly Same as 0****   Shortcut Commands
ApplicationDescriptionCategoryCategory

Conclusion

Linux is one of the most reliable operating systems. Being an open-source software it can be installed in as many computer systems for free. It works without any trouble with malware or viruses.