Linux Commands Cheat Sheet
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
Command | Description |
---|---|
head | This command is used to display the first few lines of a file |
clear | This command is used to clear the terminal window |
chmod | This command is used to change the file permissions |
pr -x | This command is used to format and print files in a specified manner |
rm filename | This command is used to delete a file |
lp -d lp -P | This command is used to specify the printer device or printer name when printing a file from the command line |
history | This command is used to display a list of previously executed commands in the terminal window |
touch | This command is used to create a new file |
gunzip | This command is used to decompress zip file |
cp | This command is used to copy files and directories |
lp -nc, lpr c | This command is used to prints “c” copies of the given File |
tail | This command is used to display the end of the file |
grep | This command is used to search for patterns in files. |
pr -h | This command is used assign a header to the fileto |
cd / | This command is used to move to the root directory |
pr -n | This command is used to specify the number of lines per page |
tar | This command is used to archive files |
sudo | This command is used to run the command with the privileges of superuser or root |
apt-get | This 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. |
gzip | This 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 < Filename | This command is used to send email with attachment |
User Management Commands of Linux
Let's see some user management commands in Linux:
Command | Description |
---|---|
sudo adduser username | This 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 USERNAME | This 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 GROUPNAME | This command is used to remove a user from a group |
sudo groupadd group_name | This command is used to add a new group |
sudo groupdel group_name | This command is used to delete a group |
su username | This command is used to switch user |
change username | This command is used to change the password aging information for a user account. |
groups | This command is used to display the groups that a user belongs to |
finger | This command is used to show the information of all the logged in users |
finger username | This command is used to give information of a particular user |
Process Commands
Now Let's see some of the process commands in Linux:
Command | Description |
---|---|
ps PID | This command is used to give the status of a particular process |
df | This command is used to display information about the file system's disk space usage |
top | This command is used to displays information about processes and their resource consumption, including CPU usage, memory usage, and system load. |
renice | This command is used to change the priority of a running process |
ps | This command is used to give the status of a running process for a user |
nohup | It 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. |
nice | This command is used set the priority of a process |
kill PID | This command is used to terminate or signal a process |
free | This command is used to display information about the system's memory usage |
fg | This command is used to run a stopped process in the foreground |
pidof | This command is used to give the process ID of a process |
bg | This command is used to send a process to the background |
Networking Commands
Now Let's see some of the process commands in Linux:
Command | Description |
---|---|
netstat | This command is used to check network statistics |
traceroute | This command is used to trace the network path between the local system and a remote system |
dir | This command is used to display files in the current directory of a remote computer |
scp | This command is used to securely copy files and directories between hosts on a network |
SSH hostname | This command is used to login into a remote Linux machine using SSH |
ifconfig | This 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 file | This command is used to upload ‘file’ from local to remote computer |
get file | This command is used to download ‘file’ from local to remote computer |
curl | This command is used to transfer data using URLs |
wget | This command is used to download files from the web. |
quit | This command is used to logout |
nslookup | This command is used to query DNS records. |
dig | This 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:
Command | Description |
---|---|
vi | This command is used to open the VI editor |
u | This command is used to undo last change |
D | This command is used to delete contents of line after the cursor |
x | This 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 |
U | This command is used to undo the last change made to the current line |
dw | This command is used to delete a word |
s | This command is used to substitute characters or strings within a line |
:q! | This command is used to quit VI editor without saving. |
ESC | This command is used to switch from insert mode to command mode |
4dw | This command is used to delete the next four words from the current cursor position |
:w | This command is used to save changes |
A | This command is used to append text to the end of the current line |
cw | This command is used to change the current word |
:wq: | This command is used to save the changes made and quit the VI editor |
3dd | This command is used to delete 3 lines |
:q | This command is used to quit VI editor |
C | This command is used to delete contents of a line after the cursor and insert new text. |
a | This command is used to write after the cursor |
S | This command is used to substitute the entire line and begin to insert cursor at the beginning of the line |
R | This command is used to overwrite the characters from cursor going forward |
i | This command is used to insert at cursor (make editor go into insert mode) |
dd | This command is used to delete line |
o | This command is used to open a new line |
r | This command is used to replace character |
File and Directory CRUD Navigation Commands
Now Let's see File and Directory CRUD navigation commands in Linux:
Command | Description |
---|---|
ls | This command is used to list the files and directories in the current directory |
ls -R | This command is used to lists all the files and the sub-directories as well |
ls -a | This command is used to list hidden files as well |
ls -al | This 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 dirname | This command is used to change the working directory to given “dirname” |
mkdir directoryname | This command is used to create a new directory in the present working directory or at the specified path |
cat filename | This command is used to display the file content |
cat f1 f2 > f3 | This command is used to concatenate two or more files and redirect the output to a new file |
cat > filename | This command is used to create a new file |
rmdir directory | This command is used to delete a directory |
mv | This 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 fileName | This 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 level | Octal Value |
---|---|
Read | 4 |
Write | 2 |
Execute | 1 |
Each Entity has a particular position in the code:
Entity | Position |
---|---|
User | left |
Group | middle |
Other | right |
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 level | Symbol |
---|---|
Read | r |
Write | w |
Execute | x |
Each Entity has a Symbol:
Entity | Symbol |
---|---|
User | u |
Group | g |
Other | o |
There are operators that are used to manipulate the permissions.
Task | Operator |
---|---|
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:
Command | Description |
---|---|
ls -l filenamr | This 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 |
r | This command is used to represent the read permission |
w | This command is used to represent the write permission |
x | This command is used to represent the execute permission |
-= | This command is used to represent no permission |
chown user | This command is used to change the ownership of a file/directory |
Chown user:group filename | This 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:
Command | Description |
---|---|
du | This command is used for disk usage and is used to estimate the space usage for a file or directory. |
lscpu | This command is used to display information about the CPU installed in the system |
lspci | This command is used to display information about the PCI devices installed in the system |
free -h | This command is used to display the free and used memory |
uname | This command is used to show system information |
dmesg | This command is used to display system messages |
lshw | This command is used to display hardware information |
cpu-info | This command is used to display the information about your CPU. |
cat /proc/meminfo | This command is used to give the information about memory like total and occupied and so on. |
lsusb -tv | This command is used to list all the USB-connected devices. |
Environment Variable Commands
Command | Description |
---|---|
source | This command is used to execute commands from a file. |
export Variable=value | This command is used to set an environment variable value |
VARIABLE_NAME= variable_value | This command is used to create a new variable |
echo $VARIABLE | This command is used to display the value of a variable |
unset | This command is used to remove a variable |
env | This 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.