Linux Cheat Sheet - Scaler Topics
Linux Cheat Sheet - Scaler Topics
$ mv filepath /path/to/destination/
LINUX
CHEAT SHEET
$ ls /path/to/directory
File and Directory Management
cd command
The cd command is used to change the current working directory in the terminal.
Let's say your root directory contains the following files and folders.
01 Cheatsheet
Change to the home directory: .
├── Desktop
Command │ ├── important_report.txt
Current Directory
│ └── meeting_minutes.docx
$ cd ..
.. $cd
└── Animals
Description
├── animals.txt
Running cd without any arguments takes you to your home ├── Cats
directory from wherever you are. │ ├── cat1.png
Previous
Working Directory │ └── cat2.png
└── Dogs
Example ├── dog1.png
$ cd └── dog2.png
02 Cheatsheet
Use relative paths:
Command Description
$ cd ../relative/path You can use relative paths to navigate to a directory relative
to your current location. .. represents the parent directory,
and you can specify additional directories to traverse from
there.
Example
Assume, your current directory is Cats directory.
../Dogs/ .
├── Desktop
│ ├── important_report.txt
Go inside the
│ └── meeting_minutes.docx
Dogs directory
..
Move up to ..
Animals Directory └── Animals
├── animals.txt $ cd ../
/Dogs/ ├── Cats
│ ├── cat1.png
│ └── cat2.png
└── Dogs
├── dog1.png
└── dog2.png
03 Cheatsheet
ls (list all directories) command
The ls command in Linux is used to list the
contents of a directory.
Description
This command lists the files and directories in the project_plan.pdf research_paper.doc
specified directory (/path/to/directory).
Command Description
$ ls -l The -l option provides detailed information like file/folder
permissions, owner, size, and modification timestamps.
Example
Assuming the current working directory is Animals:
Timestamps of file/folder creation
$ ls -l total 12
-rw-rw-r-- 1 scaler scaler 23 May 29 14:57 animals.txt
drwxrwxr-x 2 scaler scaler 4096 May 29 14:54 Cats
drwxrwxr-x 2 scaler scaler 4096 May 29 14:55 Dogs
04 Cheatsheet
List files and directories
Example
including hidden files:
Assuming the current working directory is Animals:
$ ls -a
Command
$ ls -a
Command Description
$ ls -R Lists all the files and directories inside the current directory
as well as all the files and directories of all sub-directories.
Example
Assuming the current working directory is Animals:
./Dogs:
dog1.png dog2.png
05 Cheatsheet
pwd command
Example
Command Assuming the current working directory is Animals:
$ pwd
$ pwd /home/scaler/Animals
Description
Shows the current directory path.
mkdir command
Command Description
$ mkdir This command creates a directory.
Example
Let's say we are in the root directory.
$ mkdir Pictures .
├── Desktop
│ ├── important_report.txt
│ └── meeting_minutes.docx
├── Documents
│ ├── project_plan.pdf
│ └── research_paper.doc
..
..
├── Animals
│ ├── animals.txt
│ ├── Cats
│ │ ├── cat1.png
│ │ └── cat2.png
│ └── Dogs
│ ├── dog1.png
│ └── dog2.png
└── Pictures New directory created!!
06 Cheatsheet
rmdir command
The rmdir command in Linux is used to remove directories (folders) that are empty. It can be used to delete directories
that do not contain any files or subdirectories.
Command Description
$ rmdir directory_name The rmdir command in Linux is used to remove
directories (folders) that are empty.
Example
Let's delete the Pictures directory while we are in the root directory.
07 Cheatsheet
mv command
The mv command in Linux is used to move or rename files and directories.
It allows you to change the location of a file or directory within the file system or give it a new name.
Command Description
$ mv filepath /path/to/destination/ This command moves the file with the given location to
the directory specified by /path/to/destination/.
Example
Let's move the project_plan.pdf file to Downloads folder. Assume we are in the root directory.
$ mv Documents/project_plan.pdf Downloads/
. .
├── Desktop ├── Desktop
│ ├── important_report.txt │ ├── important_report.txt
│ └── meeting_minutes.docx │ └── meeting_minutes.docx
Move project_plan.pdf
├── Documents to Downloads folder. ├── Documents
│ ├── project_plan.pdf │ └── research_paper.doc
│ └── research_paper.doc ├── Downloads
├── Downloads │ ├── project_plan.pdf
│ ├── software_installer.exe │ ├── software_installer.exe
│ └── vacation_photo_album.zip │ └── vacation_photo_album.zip
.. ..
.. ..
Rename a file:
Command Description
$ mv old_file.txt new_file.txt By specifying a different name as the destination, you can
rename the file old_file.txt to new_file.txt within the same
directory.
Example
Let's rename the project_plan.pdf which is in Documents folder. Assume currently we are in Documents folder.
$ mv project_plan.pdf project_design.pdf .
├── Desktop
│ ├── important_report.txt
│ └── meeting_minutes.docx
├── Documents
│ ├── project_design.pdf
│ └── research_paper.doc
├── Downloads
│ ├── software_installer.exe
│ └── vacation_photo_album.zip
..
..
08 Cheatsheet
cat command
The cat command in Linux is used to concatenate and display the contents of files.
It can also be used to create new files or append to existing ones.
Command Description
$ cat filename This command displays the contents of filename in
the terminal.
Example
Let's see the content of important_report.txt file.
09 Cheatsheet
Concatenate multiple files:
Command Description
$ cat file1 file2 You can concatenate multiple files by specifying their names
consecutively. The output will be the combined contents of
the files.
Example
Let's concatenate animals.txt and important_report.txt files.
Wake up at 5 AM.
Have a light breakfast.
Exercise for 30 minutes.
Commence your day-to-day activities.
cats
cows
dogs
tigers
10 Cheatsheet
Output to a new file:
Command Description
$ cat file1 file2 > combined.txt The > symbol redirects the concatenated output of file1.txt
and file2.txt to a new file named combined.txt.
Example
Let's concatenate animals.txt and important_report.txt and
output it to combined.txt which is created in the current directory.
.
├── combined.txt Contains content of both
├── Desktop important_report.txt and animals.txt files.
│ ├── important_report.txt
│ └── meeting_minutes.docx
├── Documents
│ ├── project_design.pdf
│ └── research_paper.doc
├── Downloads
│ ├── software_installer.exe
│ └── vacation_photo_album.zip
..
..
Command Description
$ cat >> existingfile.txt Anything you type will be appended to existingfile.txt.
Press Ctrl + D to save and exit.
Example
Let's add "Go to sleep by 10 PM" in the important_report.txt file.
11 Cheatsheet
grep command
The grep command in Linux is used to search for specific patterns or lines of text within files.
It allows you to filter and extract information based on specified criteria.
Command Description
$ grep pattern file This command searches for the given pattern within the
given file and displays all lines that contain the pattern.
Example
Let's search for the line which contains "breakfast" in the file important_report.txt
Output
grep "breakfast" Desktop/important_report.txt Have a light breakfast.
Output
grep "^Wake" ~/Desktop/important_report.txt Wake up at 5 AM.
Command Description
$ grep -n pattern file The -n option adds line numbers to the output, indicating
the line number of each matched line.
Example
Search for lines with line numbers that contain "a":
Output
grep -n "a" Desktop/important_report.txt
1:Wake up at 5 AM.
2:Have a light breakfast.
4:Commence your day-to-day activities.
12 Cheatsheet
Case-insensitive search:
Command Example
$ grep -i pattern file Search for lines with case-insensitive match for "am":
Description Output
Command Example
$ grep -v pattern file Search for lines that do not contain the word "Exercise":
Description Output
The -v option displays lines that do not match the specified
pattern, effectively excluding those lines from the output. Wake up at 5 AM.
Have a light breakfast.
Commence your day-to-day activities.
Go to sleep by 10 PM.
Command Description
$ grep -r pattern /path/to/directory/ The -r option searches the pattern against all the files in
the specified directory.
Example
Search for lines that do not contain the word "Exercise" in all the files inside root directory:
Assume there are only two files in the root directory: important_report.txt and animals.txt.
grep -r -v "Exercise" .
Output
./Desktop/important_report.txt:Wake up at 5 AM.
./Desktop/important_report.txt:Have a light breakfast.
./Desktop/important_report.txt:Commence your day-to-day activities.
./Desktop/important_report.txt:Go to sleep by 10 PM.
./Animals/animals.txt:cats
./Animals/animals.txt:cows
./Animals/animals.txt:dogs
./Animals/animals.txt:tigers
13 Cheatsheet
File and Directory Compression Commands
gzip command
Command Description
$ gzip file1 This command is used to compress a file with
gzip compression.
Example
Lets compress the project_plan.pdf file in Documents directory.
ls command
$ gzip project_plan.pdf project_plan.pdf.gz research_paper.doc
Basically, it has compressed project_plan.pdf and the hence, the new name is project_plan.pdf.gz
gunzip command
Command Example
$ gunzip fileDemo.gz Lets decompress the project_plan.pdf.gz file which we
created in the last step.
$ gunzip project_plan.pdf.gz
Description
ls command
This command is used to unzip a file that has
gzip compression. project_plan.pdf research_paper.doc
tar cf command
Command Description
$ tar cf myDir.tar myDir This command is used to create an uncompressed
tar archive.
Example
Lets compress the Documents directory using the command above.
ls command
Animals Desktop Documents Music Videos
combined.txt docs.tar Downloads Pictures
14 Cheatsheet
tar cfz command
Command Description
$ tar cfz demoDir.tar demoDir This command is used to create a tar archive with
gzip compression.
Example
Lets compress the Documents directory with gzip compression using the command above.
ls command
Animals Desktop Documents Music Videos
combined.txt docs.tar Downloads Pictures
Command Description
$ tar xf demoFile This command is used to extract the contents of any type
of tar archive.
Example
Let's decompress the Documents directory in the Downloads directory.
Assume current directory: Downloads
$ tar xf ../home/docs.tar
ls
15 Cheatsheet
Environment Variable Commands
env command
Command Example
$ env $ env
Description SHELL=/bin/bash
COLORTERM=truecolor
This command displays all the environment variables.
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
XDG_MENU_PREFIX=gnome-
...
...
USER=scaler
GNOME_TERMINAL_SERVICE=:1.101
DISPLAY=:0
OLDPWD=/home/scaler/
_=/usr/bin/env
echo command
Command Example
$ echo ENV_VARIABLE NAME /bin/bash
$ echo $SHELL
Description
This command displays the environment variable.
unset command
Command Description
$ unset This command removes a variable.
16 Cheatsheet
Network Configuration
ifconfig command
Command Description
$ ifconfig [interface] [options] This command is used to display information for all active
network interfaces.
Options
up or down: Enables or disables the specified interface.
hw ether [MAC address]: Sets the MAC (Media Access Control) address for the interface.
mtu [size]: Sets the Maximum Transmission Unit (MTU) for the interface.
Example
$ ifconfig docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:a4:7a:5f:60 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Set an IP address and netmask for a network interface: sudo ifconfig eth0 192.168.0.10
netmask 255.255.255.0
Set the Maximum Transmission Unit (MTU) for a network interface (e.g., eth0): sudo ifconfig eth0 mtu 1500
17 Cheatsheet
ping command
Command Description
$ ping [options] destination The ping command in Linux is used to test network
connectivity by sending ICMP echo request packets to a
specific destination.
Options
-c count: Specifies the number of ICMP echo request packets to send before stopping.
-i interval: Sets the interval (in seconds) between sending each packet.
-w deadline: Specifies the time (in seconds) to wait for a reply before stopping.
-s packetsize: Sets the size of the ICMP echo request packets to send.
-q: Quiet output, only displays summary statistics.
-v: Verbose output, displays more detailed information.
Example
Send 5 ICMP echo request packets with a packet size of 64 bytes to the IP address 8.8.8.8
Ping a domain name with a deadline of 3 seconds and display verbose output
ping -w 3 -v www.google.com
18 Cheatsheet
ssh command
Command
$ ssh [options] [user@]host [command]
Description
The ssh command is used in Linux to establish a secure shell (SSH) connection to a remote server or device.
It allows you to securely log in to a remote system and execute commands or transfer files over an encrypted connection.
Example
ssh command
19 Cheatsheet
Users and Permissions
adduser command
Command Description
$ sudo adduser username This command is used to add a user.
Example
This command is used to add a user.
passwd command
Command Description
$ sudo passwd -l ‘username’ This command is used to change the password of a user.
Example
$ sudo passwd -l scalertopics passwd: password expiry information changed.
20 Cheatsheet
userdel command
Command Description
$ sudo userdel -r ‘username’ This command is used to remove a newly created user.
Example
sudo userdel -r 'scalertopics'
usermod command
Command Description
$ sudo usermod -a -G GROUPNAME USERNAME This command is used to add a user to a particular group.
Example
The command to add scalertopics to root is shown.
deluser command
Command Description
$ sudo deluser USER GROUPNAME This command is used to remove a user from a group.
Example
The command to remove scalertopics from the root group is shown.
$ sudo deluser scalertopics root Removing user `scalertopics' from group `root' ...
Done.
scalertopics is the username root is the groupname
21 Cheatsheet
finger command
Command Description
$ finger This command shows the information of all the users
logged in.
Example
$ finger Login Name Tty Idle Login Time Office Office Phone
scalertopics Scaler Academy *:0 May 29 13:12 BLR, NA
Command Description
$ finger username This command gives information about a particular user.
Example
$ finger scalertopics Login: scalertopics Name: Scaler Academy
Directory: /home/scalertopics Shell: /bin/bash
Office: BLR, NA Home Phone: NA
Never logged in.
No mail.
No Plan.
22 Cheatsheet
File Permissions
ls -l filename command
Command Description
$ ls -l filename This command is used to show the file permissions along
with the owner and other details of the specified file.
Example
$ ls -l important_report.txt
r w - r w - r - -
23 Cheatsheet
chmod command
The chmod command stands for “change-mode” which means that using this command, we can change the mode in
which some user is able to access the file.
Command
$ chmod [ugoa…][-+=]perms…[,....] FILE….
Description
Notations
Example
Let's add read permission to the other class for the file: important_report.txt
Current directory: Desktop
chmod
command
Add File
Give permission permission Give read
to other class permission
Let's add read permission to the other class for the file: important_report.txt
Current directory: Desktop
chmod
command
Remove File
Remove permission permission Remove
from all classes write permission
i.e. user, group and other
24 Cheatsheet
Numerical Method
r (read) = 4
w (write) = 2
x (execute) = 1
No permissions = 0
Example
If a user has read and write permission, then
read+write no permission
permission to user to group
read+write
permission to group
equivalent to
$ chmod ugo+rw important_report.txt $ chmod 666 important_report.txt
25 Cheatsheet
System Information Commands
history command
Command Description
$ history This command displays the list of all the typed commands
in the current terminal session.
Example
$ history 1970 cd ../Animals/Dogs/
1971 cd ../Dogs/
1972 clear
1973 cd ..
1974 clear
1975 ls
1976 ls /home/captain/home/Documents
...
...
...
2018 clear
2019 cd Desktop/
2020 history
clear command
Command Description
$ clear Clears the terminal i.e. no previous command will be
visible on the screen now.
Example
$ history $ |
...
...
1970 cd ../Animals/Dogs/
1971 cd ../Dogs/
1972 clear
1973 cd ..
clear command
1974 clear
1975 ls
1976 ls /home/captain/home/Documents
...
...
...
2018 clear
2019 cd Desktop/
2020 history
26 Cheatsheet
hostname command hostid command
Command Command
$ hostname $ hostid
Description Description
Shows the name of the system host. Shows the name of the system host.
Example Example
$ hostname scaler $ hostid 520d3428
sudo command
Command Description
$ sudo Allows a regular user to run the programs with the security
privileges of a superuser or root.
Example
You can not run the command $ apt-get update without administrator-level access.
$ apt-get update
However, using the sudo command, you can perform the required operation.
27 Cheatsheet
apt-get command
Command Example
$ apt-get $ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install package_name
Description
$ sudo apt-get purge package_name
This command is used to:
$ sudo apt-get remove package_name
* Update package lists
* Upgrade installed packages
* Install a package
* Remove a package
* Purge a package (remove package and its configuration
files)
* Search for a package
* Show information about a package
* List installed packages
* Upgrade the distribution to a new release
* Fix broken dependencies
* Clean the local repository cache
date command
Command Example
$ date Monday 29 May 2023 10:47:58 PM IST
Description
This command is used to show the current date and time.
cal command
Command Example
$ cal May 2023
Su Mo Tu We Th Fr Sa
Description 1 2 3 4 5 6
7 8 9 10 11 12 13
Shows the calendar of the current month.
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
28 Cheatsheet
whoami command
Command Example
$ whoami $ whoami scaler
Description
This command displays the name with which you are
logged in.
whereis command
Command Description
$ whereis [options] command The "whereis" command in Linux is used to locate the
binary, source code, and manual page files for a given
command or program.
Options
"-b": Searches only for executable files.
"-m": Searches only for manual page files.
"-s": Searches only for source code files.
"-u": Reports files that are unknown to the system's database
(located outside thestandard directories).
Example
Searching for the binary and manual page of the "gcc" command
$ whereis -b -m gcc
29 Cheatsheet
Process Commands
bg command
Command Example
$ bg %ID Move a specific job with job ID 2 to the background:
Description
This command is used to send a process to the $ bg %2
background.
fg command
Command Example
$ fg %ID Move a specific job with job ID 2 to the background:
Description
This command is used to run a stopped process in the $ fg %1
background.
top command
Command Description
$ top This command is used to get the details of all active
processes.
Example
$ top PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2392 scaler 20 0 4652336 292348 106788 S 9.3 3.7 8:11.99 gnome-shell
2252 scaler 20 0 526384 84936 48076 S 8.9 1.1 6:06.34 Xorg
6206 scaler 20 0 816444 52048 39088 S 8.3 0.7 0:33.52 gnome-terminal-
237 root -51 0 0 0 0 S 3.0 0.0 1:38.87 irq/128-ELAN066
1097 mysql 20 0 2380348 397260 37608 S 1.0 5.0 1:34.41 mysqld
97073 scaler 20 0 11992 3968 3188 R 0.7 0.1 0:00.25 top
40 root 20 0 0 0 0 S 0.3 0.0 0:03.56 ksoftirqd/4
877 root 20 0 273896 10636 9620 S 0.3 0.1 0:07.36 thermald
882 root 20 0 1473848 41016 14796 S 0.3 0.5 0:46.55 warp-svc
1088 postgres 20 0 218288 29064 27028 S 0.3 0.4 0:00.25 postgres
1454 root 20 0 1374764 12380 10756 S 0.3 0.2 0:16.54 teamviewerd
2443 scaler 20 0 162836 7544 6756 S 0.3 0.1 0:04.84 at-spi2-registr
2622 scaler 20 0 617944 4200 3864 S 0.3 0.1 0:10.68 warp-taskbar
30 Cheatsheet
ps command
Command Example
$ ps
$ ps PID TTY TIME CMD
6214 pts/0 00:00:00 bash
Description 97206 pts/0 00:00:00 ps
The "ps" command in Linux is used to provide information
about the currently running processes on the system.
ps PID command
Command Example
$ ps PID $ ps 1
Description
This command gives the status of a particular process. PID TTY STAT TIME COMMAND
1 ? Ss 0:02 /sbin/init splash
pidof command
Command Example
$ pidof PROCESSNAME $ pidof bash 6214
Description
This command is used to give the process ID of a
particular process.
31 Cheatsheet
Hardware Configuration Commands
cpu-info command
Command Description
$ cpu-info This command is used to display the information about
your CPU. It can be used after installation of the necessary
package using sudo apt install cpuinfo.
Example
$ cpu-info Packages:
0: Intel Core i5-8265U
Cores:
0: 2 processors (0-1), Intel Kaby Lake
1: 2 processors (2-3), Intel Kaby Lake
2: 2 processors (4-5), Intel Kaby Lake
3: 2 processors (6-7), Intel Kaby Lake
Logical processors:
0: APIC ID 0x00000000
1: APIC ID 0x00000001
2: APIC ID 0x00000002
3: APIC ID 0x00000003
4: APIC ID 0x00000004
5: APIC ID 0x00000005
6: APIC ID 0x00000006
7: APIC ID 0x00000007
free -h command
Command Description
$ free -h This command is used to display the free and used
memory. The -h flag is used for converting the information
(to be displayed) to human-readable form.
Example
$ cpu-info
32 Cheatsheet
lsusb -tv command
Command Description
$ lsusb -tv List all the USB connected devices.
Example
Command Description
$ cat /proc/meminfo Gives the information about memory like total and
occupied and so on.
Example
33 Cheatsheet
du command
Command Description
$ du -h file/folder This command stands for disk usage and is used to
estimate the space usage for a file or directory.
Example
34 Cheatsheet