Linux Commands Cheat Sheet

Topics Covered

Overview

The command line interface (CLI) is a text-based interface that lets users interact with the Linux operating system by typing commands in a Linux terminal window. This interface is vital for Linux users and administrators since it provides better flexibility and speed than GUIs. Users may utilize the CLI to manage files and folders, as well as configure system settings and network connections. Users may use the full power of the Linux operating system and complete difficult operations more effectively by utilizing the CLI.

The cheatsheet is divided into parts of distinct set of instructions. The article discusses fundamental commands in Linux that are required for typical activities such as creating and removing files and directories, transferring data, and examining system information. It also goes through user administration commands, which allow users to modify their user accounts and rights on Linux system.

Cheatsheet for Linux Commands

Basic Linux Commands

Let's see some basic commands in Linux

CommandDescription
headThis command is used to display the first few lines of a file
clearThis command is used to clear the terminal window
chmodThis command is used to change the file permissions
pr -xThis command is used to format and print files in a specified manner
rm filenameThis command is used to delete a file
lp -d lp -PThis command is used to specify the printer device or printer name when printing a file from the command line
historyThis command is used to display a list of previously executed commands in the terminal window
touchThis command is used to create a new file
gunzipThis command is used to decompress zip file
cpThis command is used to copy files and directories
lp -nc, lpr cThis command is used to prints “c” copies of the given File
tailThis command is used to display the end of the file
grepThis command is used to search for patterns in files.
pr -hThis command is used assign a header to the fileto
cd /This command is used to move to the root directory
pr -nThis command is used to specify the number of lines per page
tarThis command is used to archive files
sudoThis command is used to run the command with the privileges of superuser or root
apt-getThis command is used to interact with the Advanced Package Tool (APT) library and allows users to easily download and install software packages from the official repositories or other sources.
gzipThis command is used to compress files
mail -s 'subject' -c 'cc-address' -b 'bcc-address' 'to-address'This command is used to send email
mail -s "Subject" to-address < FilenameThis command is used to send email with attachment

User Management Commands of Linux

Let's see some user management commands in Linux:

CommandDescription
sudo adduser usernameThis command is used to add a new user
sudo passwd -l 'username'This command is used to change the password of a user
sudo usermod -a -G GROUPNAME USERNAMEThis command is used to add a new user to a group
sudo userdel -r 'username'This command is used to remove a newly created user
sudo deluser USER GROUPNAMEThis command is used to remove a user from a group
sudo groupadd group_nameThis command is used to add a new group
sudo groupdel group_nameThis command is used to delete a group
su usernameThis command is used to switch user
change usernameThis command is used to change the password aging information for a user account.
groupsThis command is used to display the groups that a user belongs to
fingerThis command is used to show the information of all the logged in users
finger usernameThis command is used to give information of a particular user

Process Commands

Now Let's see some of the process commands in Linux:

CommandDescription
ps PIDThis command is used to give the status of a particular process
dfThis command is used to display information about the file system's disk space usage
topThis command is used to displays information about processes and their resource consumption, including CPU usage, memory usage, and system load.
reniceThis command is used to change the priority of a running process
psThis command is used to give the status of a running process for a user
nohupIt stands for "no hang up" and it is typically used to execute long-running processes that should not be terminated if the shell session is closed.
niceThis command is used set the priority of a process
kill PIDThis command is used to terminate or signal a process
freeThis command is used to display information about the system's memory usage
fgThis command is used to run a stopped process in the foreground
pidofThis command is used to give the process ID of a process
bgThis command is used to send a process to the background

Networking Commands

Now Let's see some of the process commands in Linux:

CommandDescription
netstatThis command is used to check network statistics
tracerouteThis command is used to trace the network path between the local system and a remote system
dirThis command is used to display files in the current directory of a remote computer
scpThis command is used to securely copy files and directories between hosts on a network
SSH hostnameThis command is used to login into a remote Linux machine using SSH
ifconfigThis command is used to display the network configuration of the system
Ping hostname=""This command is used to ping and Analyze network and host connections
put fileThis command is used to upload ‘file’ from local to remote computer
get fileThis command is used to download ‘file’ from local to remote computer
curlThis command is used to transfer data using URLs
wgetThis command is used to download files from the web.
quitThis command is used to logout
nslookupThis command is used to query DNS records.
digThis command is used to query the DNS records of a domain name

VI Editing Commands

Now Let's see some VI Editing commands in Linux:

CommandDescription
viThis command is used to open the VI editor
uThis command is used to undo last change
DThis command is used to delete contents of line after the cursor
xThis command is used to delete character at the cursor
~This command is used to change case of individual character
:x:This command is used to save the changes made and quit the VI editor
UThis command is used to undo the last change made to the current line
dwThis command is used to delete a word
sThis command is used to substitute characters or strings within a line
:q!This command is used to quit VI editor without saving.
ESCThis command is used to switch from insert mode to command mode
4dwThis command is used to delete the next four words from the current cursor position
:wThis command is used to save changes
AThis command is used to append text to the end of the current line
cwThis command is used to change the current word
:wq:This command is used to save the changes made and quit the VI editor
3ddThis command is used to delete 3 lines
:qThis command is used to quit VI editor
CThis command is used to delete contents of a line after the cursor and insert new text.
aThis command is used to write after the cursor
SThis command is used to substitute the entire line and begin to insert cursor at the beginning of the line
RThis command is used to overwrite the characters from cursor going forward
iThis command is used to insert at cursor (make editor go into insert mode)
ddThis command is used to delete line
oThis command is used to open a new line
rThis command is used to replace character

File and Directory CRUD Navigation Commands

Now Let's see File and Directory CRUD navigation commands in Linux:

CommandDescription
lsThis command is used to list the files and directories in the current directory
ls -RThis command is used to lists all the files and the sub-directories as well
ls -aThis command is used to list hidden files as well
ls -alThis command is used to display the contents of the directory in a long list format, including any hidden files and directories
cd or cd ~This command is used to change the current working directory
cd ..This command is used to navigate up one directory level in the file system
cd dirnameThis command is used to change the working directory to given “dirname”
mkdir directorynameThis command is used to create a new directory in the present working directory or at the specified path
cat filenameThis command is used to display the file content
cat f1 f2 > f3This command is used to concatenate two or more files and redirect the output to a new file
cat > filenameThis command is used to create a new file
rmdir directoryThis command is used to delete a directory
mvThis command is used to move or rename files and directories.
mv file "new file path"This command is used to move the file to the new location
find <starting position to search> <expression determining what to find> <options> <what to find>This command is used for walking a file hierarchy. It is used to find files/directories and perform operations on them.
grep <options> pattern fileNameThis command searches a file for a particular pattern of characters and displays all the lines that contain that pattern.

Linux File Permissions Commands

Before looking at the File Permission commands, let's first understand what are File Permissions in linux:

File Permissions in Linux refer to the access rights assigned to files and directories in the Linux operating system. These file permissions in linux can be used to control the ability of users and processes to read, write, and execute files on the system.

Three basic types of permissions can be assigned to a file or directory in Linux:

  • Read Permission (r):
    This type of file permissions in linux grants the user or the group the ability to view the contents of a file or directory.
  • Write Permission (w):
    This type of file permissions in linux grants the user or group the ability to modify or delete a file or directory. Users or a group can have the ability to add, remove, and rename files stored in a directory if they have write permission. Think about a situation where the user needs to have write access to a file but not to the directory where it is stored. The contents of the file can be changed. However, the user won't be able to rename the file, move it, or delete it from the directory.
  • Execute Permission (x):
    This type of file permissions in linux grants the user or group the ability to execute a file or change it into a directory. If the read and write permissions are set, you might still be able to view and modify the program code without the execute permission, but in Linux, you won't be able to run it.

Each of the three permissions — read (4), write (2), and execute (1) has a corresponding numerical number. The level of permission for a file is determined by adding the values of its permissions.

Each access level (read, write, execute) has an octal value:

Access levelOctal Value
Read4
Write2
Execute1

Each Entity has a particular position in the code:

EntityPosition
Userleft
Groupmiddle
Otherright

In linux, we can also set permissions in symbolic mode by combining letters and symbols. The symbols denote the action to be made (+ to add a permission, - to remove a permission, and = to set the permission), while the letters denote the degree of authorization (u for owner, g for group, o for others, and a for all).

Each access level (read, write, execute) has a symbol:

Access levelSymbol
Readr
Writew
Executex

Each Entity has a Symbol:

EntitySymbol
Useru
Groupg
Othero

There are operators that are used to manipulate the permissions.

TaskOperator
Grant Access+
Remove Access-
Set a level of Access=

Now that we know how the file permissions works in linux, let take a look at the commands that we can use in linux:

CommandDescription
ls -l filenamrThis is used to list the contents of a directory in a long format, providing additional information about the files and directories in the specified directory
rThis command is used to represent the read permission
wThis command is used to represent the write permission
xThis command is used to represent the execute permission
-=This command is used to represent no permission
chown userThis command is used to change the ownership of a file/directory
Chown user:group filenameThis command is used to change the user as well as group for a file or directory

Hardware Information Commands

Let's take a look at some of the hardware information commands in Linux that provide us with information about the hardware we're utilizing:

CommandDescription
duThis command is used for disk usage and is used to estimate the space usage for a file or directory.
lscpuThis command is used to display information about the CPU installed in the system
lspciThis command is used to display information about the PCI devices installed in the system
free -hThis command is used to display the free and used memory
unameThis command is used to show system information
dmesgThis command is used to display system messages
lshwThis command is used to display hardware information
cpu-infoThis command is used to display the information about your CPU.
cat /proc/meminfoThis command is used to give the information about memory like total and occupied and so on.
lsusb -tvThis command is used to list all the USB-connected devices.

Environment Variable Commands

CommandDescription
sourceThis command is used to execute commands from a file.
export Variable=valueThis command is used to set an environment variable value
VARIABLE_NAME= variable_valueThis command is used to create a new variable
echo $VARIABLEThis command is used to display the value of a variable
unsetThis command is used to remove a variable
envThis command is used to display all environment variables

Conclusion

  • CLI interface is vital for Linux users and administrators since it provides better flexibility and speed than GUIs.
  • In this article, we looked at a complete cheatsheet for Linux commands used in the Command-Line Interface (CLI).
  • We divided the cheatsheet into the following sections based on the functionalities:
    • Basic Linux commands
    • User management commands
    • Process commands
    • Networking commands
    • VI editing commands
    • File and Directory CRUD navigation commands
    • File permission commands
    • Hardware information commands
    • Environment variable commands
  • After mastering this cheatsheet, user can easily perform all the CLI operations and make full use of CLI.