Linux Commands Handbook
Prashant Gohel | @DevOpsWithUs
A comprehensive guide to essential Linux commands, complete with
descriptions, real-world use cases, and practical examples.
Linux Commands @DevOpsWithUs Prashant Gohel 1
Table of Contents
• Basic Linux – awk • Package Management
Commands – sed – apt
– pwd – cut – yum/dnf
– ls – tr – snap
– cd – xargs – rpm
– touch – ln • Disk & File System
– mkdir – free – mount/umount
– rm – iostat – fsck
– rmdir – netstat – mkfs
– cp – ip – blkid
– mv – iptables – lsblk
– cat – systemctl – parted
– echo – journalctl • Scripting and
– clear – crontab Automation
• Intermediate Linux – at – bash
Commands • Networking – sh
– chmod Commands – cron
– chown – ip addr – alias
– find – ip route – source
– grep – traceroute • Development and
– wc – nslookup Debugging
– head – dig – gcc
– tail – ssh – make
– sort – nmap – strace
– uniq – netcat (nc) – gdb
– diff • File Management & – git
– tar Search – vim/nano
– zip/unzip – locate • Other Useful
– df – stat Commands
– du – tree – whoami
– top – file – users
– ps – basename – hostname
– kill – dirname – env
– ping • System Monitoring – export
– wget – vmstat – date
– curl – htop – cal
– scp – lsof – man
– rsync – dmesg – history
• Advanced Linux – uptime – uname
Commands – iotop
Linux Commands @DevOpsWithUs Prashant Gohel 2
Basic Linux Commands
1. pwd Examples:
• Description: Shows you the full path of # Navigate to the /home/user/documents di
the directory you are currently in. pwd rectory
stands for “print working directory”. cd /home/user/documents
• Real-World Use Case: When you’ve # Go back to the previous directory you w
navigated through several directories and ere in
need to know your exact location in the cd -
filesystem, especially before running
4. touch
commands that depend on the current
path. • Description: Creates a new, empty file. If
the file already exists, it updates its
Examples: timestamp without changing the content.
# Display the current working directory • Real-World Use Case: Quickly creating a
pwd new file for notes, a script, or as a
# Store the current directory path in a v placeholder. Also used in scripts to
ariable for use in a script update a file’s modification time.
current_dir=$(pwd)
echo "I am currently in: $current_dir" Examples:
# Create a new empty file named my_notes.
2. ls
txt
• Description: Lists the files and touch my_notes.txt
directories inside your current directory. # Create multiple files at once
• Real-World Use Case: To see what files touch file1.txt file2.html file3.js
are in a folder, check their permissions,
sizes, and modification dates. It’s one of 5. mkdir
the most frequently used commands. • Description: Creates a new directory (or
folder). mkdir stands for “make
Examples: directory”.
# List all files and directories in the c • Real-World Use Case: To organize your
urrent location, including hidden ones, i files by creating new folders for projects,
n a detailed format documents, or photos.
ls -la
# List files in a human-readable format ( Examples:
e.g., KB, MB, GB) and sort them by modifi # Create a new directory named "Project_A
cation time (newest first) lpha"
ls -lht mkdir Project_Alpha
# Create a nested directory structure in
3. cd
one go
• Description: Changes your current mkdir -p Projects/2024/Q1
directory to a different one. cd stands for
“change directory”.
• Real-World Use Case: To navigate
through the folders on your system to get
to a specific file or run a program.
Linux Commands @DevOpsWithUs Prashant Gohel 3
6. rm 9. mv
• Description: Deletes files or directories. • Description: Moves a file or directory
Be careful, as this action is permanent. rm from one location to another. It is also
stands for “remove”. used to rename files and directories. mv
• Real-World Use Case: To clean up old stands for “move”.
files, delete logs, or remove unnecessary • Real-World Use Case: To reorganize files
folders. by moving them into different folders, or
to give a file a more descriptive name.
Examples:
Examples:
# Forcefully remove a file without being
prompted for confirmation # Rename the file "draft.txt" to "final_r
rm -f old_report.txt eport.txt"
# Recursively and forcefully remove a dir mv draft.txt final_report.txt
ectory and all its contents # Move the file "image.jpg" into the "/ho
rm -rf old_project/ me/user/Pictures" directory
mv image.jpg /home/user/Pictures/
7. rmdir
• Description: Deletes empty directories. It 10. cat
will not work if the directory contains any • Description: Displays the entire content
files or other folders. of a file on the screen. cat comes from
• Real-World Use Case: To safely remove a “catenate,” as it can also be used to join
directory that you know should be empty, files together.
preventing accidental deletion of files. • Real-World Use Case: To quickly view
the contents of a small configuration file
Examples: or a short text file without opening an
# Remove an empty directory named "temp_f editor.
iles"
rmdir temp_files Examples:
# Remove multiple empty directories at on # Display the content of the file "shoppi
ce ng_list.txt"
rmdir empty_folder1 empty_folder2 cat shopping_list.txt
# Combine two files and save the result i
8. cp nto a new file
• Description: Copies files or directories cat part1.txt part2.txt > full_document.t
from one location to another. cp stands xt
for “copy”.
• Real-World Use Case: To back up a file 11. echo
before editing it, or to copy a directory of • Description: Displays a line of text or a
project files to a new location. variable’s value in the terminal.
• Real-World Use Case: Used in shell
Examples: scripts to print status messages, debug
# Copy a file named "config.txt" to a new information, or write content to a file.
file named "config.bak" in the same direc
Examples:
tory
cp config.txt config.bak # Print a simple "Hello, World!" message
# Recursively copy an entire directory "P to the terminal
rojectX" to the "/backups/" directory echo "Hello, World!"
cp -r ProjectX /backups/ # Append a new line of text to a file
echo "This is a new line." >> my_log.txt
Linux Commands @DevOpsWithUs Prashant Gohel 4
12. clear Examples:
• Description: Clears all the text from your # Clear the terminal screen
terminal screen, giving you a clean slate. clear
• Real-World Use Case: When your # Clear the screen and then list the file
terminal is cluttered with output from s in the current directory
previous commands and you want a fresh, clear && ls -l
clean view.
Intermediate Linux Commands
13. chmod 15. find
• Description: Changes the access • Description: Searches for files and
permissions (read, write, execute) of files directories in a directory hierarchy based
and directories. chmod stands for “change on criteria like name, size, modification
mode”. time, etc.
• Real-World Use Case: To make a script • Real-World Use Case: To find a specific
executable so you can run it, or to make a file you can’t remember the location of, or
file read-only to prevent accidental to find all files of a certain type (e.g., all
changes. .log files) to delete them.
Examples: Examples:
# Make a shell script executable for the # Find all files in the current directory
owner of the file and subdirectories with the ".log" extens
chmod u+x my_script.sh ion
# Set permissions so the owner can read/w find . -name "*.log"
rite/execute, group members can read/exec # Find all directories named "temp" insid
ute, and others can only read e the /home directory
chmod 754 important_file.txt find /home -type d -name "temp"
14. chown 16. grep
• Description: Changes the owner and • Description: Searches for a specific
group of a file or directory. chown stands pattern of text inside files. grep stands for
for “change owner”. “global regular expression print”.
• Real-World Use Case: When you need to • Real-World Use Case: To find a specific
transfer ownership of files to another error message in a log file, or to find a file
user, for example, after copying files from that contains a particular function name
a system administrator account to a in a codebase.
user’s home directory.
Examples:
Examples:
# Search for the word "error" in the syst
# Change the owner of "file.txt" to the u
em log file, ignoring case
ser "john"
grep -i "error" /var/log/syslog
chown john file.txt
# Recursively search for the text "API_KE
# Recursively change the owner and group
Y" in all files in the current directory
of the "/var/www/html" directory to "www-
grep -r "API_KEY" .
data"
chown -R www-data:www-data /var/www/html
Linux Commands @DevOpsWithUs Prashant Gohel 5
17. wc 20. sort
• Description: Counts the number of lines, • Description: Sorts the lines of a text file
words, and characters in a file. wc stands alphabetically or numerically.
for “word count”. • Real-World Use Case: To sort a list of
• Real-World Use Case: To quickly check names in a file alphabetically or to sort a
the length of a document or to count how list of numbers from smallest to largest.
many lines are in a log file.
Examples:
Examples:
# Sort the lines in "names.txt" alphabeti
# Count the number of lines in "document. cally
txt" sort names.txt
wc -l document.txt # Sort a file of numbers in numeric order
# Count the number of words in a file sort -n numbers.txt
wc -w document.txt
21. uniq
18. head • Description: Removes duplicate adjacent
• Description: Displays the first few lines lines from a sorted file. It’s important to
of a file. By default, it shows the first 10 use sort first.
lines. • Real-World Use Case: To get a list of
• Real-World Use Case: To quickly peek at unique visitors from a web server log
the beginning of a large file to see what after sorting the IP addresses.
it’s about, without opening the whole
thing. Examples:
# Count the occurrences of each line in a
Examples:
file (after sorting)
# Show the first 5 lines of a log file sort data.txt | uniq -c
head -n 5 access.log # Show only the duplicate lines from a so
# Display the first 20 lines of a CSV fil rted file
e sort data.txt | uniq -d
head -n 20 data.csv
22. diff
19. tail • Description: Compares two files line by
• Description: Displays the last few lines of line and shows the differences between
a file. By default, it shows the last 10 lines. them.
• Real-World Use Case: To monitor a log • Real-World Use Case: To see what
file in real-time to see new entries as they changes have been made between two
are added. versions of a configuration file or a source
code file.
Examples:
Examples:
# Show the last 20 lines of the system lo
g # Compare "config_v1.txt" and "config_v2.
tail -n 20 /var/log/syslog txt" and show the differences
# Follow a log file in real-time to see n diff config_v1.txt config_v2.txt
ew lines as they are written # Show a "unified" diff, which is often e
tail -f application.log asier to read
diff -u old_file.txt new_file.txt
Linux Commands @DevOpsWithUs Prashant Gohel 6
23. tar 26. du
• Description: Creates or extracts files • Description: Shows the disk space used
from a .tar archive, also known as a by files and directories. du stands for
tarball. It bundles multiple files into a “disk usage”.
single file. • Real-World Use Case: To find out which
• Real-World Use Case: To archive a folders are taking up the most space on
whole project directory into a single file your disk.
for backup or transfer.
Examples:
Examples:
# Show the size of the current directory
# Create a gzipped tar archive of the "my and its subdirectories in a human-readabl
_project" directory e format
tar -czvf my_project.tar.gz my_project/ du -h
# Extract the contents of a tarball into # Show a summarized, human-readable total
the current directory for the current directory
tar -xzvf backup.tar.gz du -sh .
24. zip/unzip 27. top
• Description: Compresses files into a .zip • Description: Displays a real-time,
archive or extracts files from one. dynamic view of the running processes on
• Real-World Use Case: To compress files your system. It shows CPU usage, memory
to save space or to bundle them for easy usage, and more.
sharing with others, especially on non- • Real-World Use Case: To identify which
Linux systems. programs are using the most CPU or
memory, especially when your system is
Examples: slow.
# Zip a directory and its contents Examples:
zip -r archive.zip my_folder/
# Unzip a file into the current directory # Start the top utility
unzip archive.zip top
# Run top and show processes for a specif
25. df ic user
• Description: Shows the amount of disk top -u username
space used and available on your
filesystems. df stands for “disk free”. 28. ps
• Real-World Use Case: To check if your • Description: Shows a snapshot of the
hard drive is getting full. currently active processes.
• Real-World Use Case: To find the
Examples: Process ID (PID) of a program so you can
# Display disk space usage in a human-rea terminate it, or to check if a specific
dable format (KB, MB, GB) program is running.
df -h
Examples:
# Show usage for a specific filesystem, l
ike the root filesystem # List all running processes on the syste
df -h / m in a detailed format
ps aux
# Find a specific process by name (e.g.,
find the firefox process)
ps aux | grep firefox
Linux Commands @DevOpsWithUs Prashant Gohel 7
29. kill 32. curl
• Description: Sends a signal to a process, • Description: A versatile tool to transfer
typically to terminate it. You need the data from or to a server, using various
Process ID (PID) of the process. protocols. It’s often used for testing APIs.
• Real-World Use Case: To force-quit an • Real-World Use Case: To download a
application that has frozen or is not file, test if a web API is working, or send
responding. data to a web server.
Examples: Examples:
# Terminate a process with PID 1234 grace # Fetch the content of a webpage and disp
fully lay it in the terminal
kill 1234 curl https://example.com
# Forcefully terminate a process with PID # Download a file from a URL and save it
5678 (this is more aggressive) with its original name
kill -9 5678 curl -O [https://example.com/largefile.is
o](https://example.com/largefile.iso)
30. ping
• Description: Sends a small packet of data 33. scp
to a network host (like a website) to see if • Description: Securely copies files
it’s reachable and how long it takes for between two computers on a network
the packet to come back. using the SSH protocol. scp stands for
• Real-World Use Case: To check if you “secure copy”.
have a working internet connection or to • Real-World Use Case: To upload a file
see if a specific server is online. from your local machine to a remote
server, or to download a file from a
Examples: server.
# Ping google.com to check for a response Examples:
ping google.com
# Ping a server only 4 times and then sto # Copy a local file to a remote server
p scp /path/to/local/file.txt user@remote_h
ping -c 4 8.8.8.8 ost:/path/to/remote/directory/
# Copy a file from a remote server to you
31. wget r local machine
• Description: A command-line utility for scp user@remote_host:/path/to/remote/file
downloading files from the internet. .txt /path/to/local/directory/
• Real-World Use Case: To download a file
34. rsync
directly to your server, or to download
files in the background. • Description: Efficiently synchronizes
files and directories between two
Examples: locations. It only copies the parts of files
that have changed.
# Download a file from a URL
wget [https://example.com/somefile.zip](h • Real-World Use Case: For making
ttps://example.com/somefile.zip) backups, as it’s much faster than cp for
# Download a file and save it with a diff subsequent backups of large directories.
erent name
wget -O my_download.zip https://example.c Examples:
om/somefile.zip # Synchronize a local directory with a re
mote one, showing progress
rsync -avz --progress /local/path/ user@r
emote_host:/remote/path/
Linux Commands @DevOpsWithUs Prashant Gohel 8
# Do a "dry run" to see what files would ing them
be transferred without actually transferr rsync -avzn /local/path/ /backup/path/
Advanced Linux Commands
35. awk Examples:
• Description: A powerful pattern- # Extract the first field (username) from
scanning and text-processing language. It the password file
can process data, perform calculations, cut -d':' -f1 /etc/passwd
and generate reports. # Extract characters 2 through 5 from eac
• Real-World Use Case: To extract specific h line of a file
columns of data from a text file or log, or cut -c2-5 some_data.txt
to calculate sums and averages from
38. tr
structured data.
• Description: Translates or deletes
Examples: characters from a stream of text.
# Print the first and third columns of a • Real-World Use Case: To change all
space-separated file lowercase letters in a file to uppercase, or
awk '{print $1, $3}' data.txt to replace spaces with tabs.
# Sum the values in the second column of
a file Examples:
awk '{s+=$2} END {print s}' numbers.txt # Convert all lowercase letters to upperc
ase
36. sed cat file.txt | tr 'a-z' 'A-Z'
• Description: A stream editor for filtering # Replace all spaces with newlines, putti
and transforming text. It can perform text ng each word on a new line
substitutions, deletions, and insertions on cat sentence.txt | tr ' ' '\n'
a file or a stream of data.
39. xargs
• Real-World Use Case: To find and
replace a word in a file, or to delete all • Description: Builds and executes
lines containing a specific pattern. command lines from standard input. It
takes the output of one command and
Examples: uses it as the input (arguments) for
another.
# Replace all occurrences of "apple" with
"orange" in a file • Real-World Use Case: To delete all files
sed 's/apple/orange/g' fruits.txt with a .log extension that were found
# Delete all lines that contain the word using the find command.
"error"
sed '/error/d' logfile.txt Examples:
# Find all .tmp files and delete them usi
37. cut
ng rm
• Description: Removes sections find . -name "*.tmp" | xargs rm
(columns) from each line of a file. It’s # Find all JPEG files and archive them wi
simpler than awk for basic column th tar
extraction. find . -name "*.jpg" | xargs tar -czvf im
• Real-World Use Case: To extract a list of ages.tar.gz
usernames from the /etc/passwd file,
which uses colons as delimiters.
Linux Commands @DevOpsWithUs Prashant Gohel 9
40. ln 43. netstat
• Description: Creates links between files. • Description: Displays network
A “hard link” is a direct reference to the connections, routing tables, interface
file data, while a “symbolic link” (or statistics, and more. (Note: ss is the
symlink) is a pointer to the file’s path. modern replacement).
• Real-World Use Case: To create a • Real-World Use Case: To see which
shortcut to a program in a different programs are listening for network
directory, or to have a file appear in connections on your server or to check
multiple locations without duplicating it. active network connections.
Examples: Examples:
# Create a symbolic link to a file # List all listening TCP and UDP ports
ln -s /path/to/original/file.txt /path/to netstat -tuln
/link.txt # Show the routing table
# Create a hard link to a file netstat -r
ln /path/to/original/file.txt /path/to/ha
rdlink.txt 44. ip
• Description: The modern tool for
41. free showing and manipulating routing,
• Description: Displays the amount of free network devices, interfaces, and tunnels.
and used memory in the system, including It replaces older tools like ifconfig and
physical memory (RAM) and swap space. route.
• Real-World Use Case: To quickly check if • Real-World Use Case: To find your
your system is running out of memory. computer’s IP address or to configure
network settings from the command line.
Examples:
# Display memory usage in a human-readabl Examples:
e format (MB, GB) # Show the IP addresses of all network in
free -h terfaces
# Display memory usage every 2 seconds ip addr show
free -s 2 # Show the system's routing table
ip route show
42. iostat
• Description: Reports Central Processing 45. iptables
Unit (CPU) statistics and input/output • Description: A powerful tool for
statistics for devices and partitions. configuring the Linux kernel firewall. It
• Real-World Use Case: To identify allows you to set up rules for filtering
performance bottlenecks by checking if network traffic.
your storage devices (hard drives, SSDs) • Real-World Use Case: To block incoming
are being overworked. traffic from a specific IP address or to
allow traffic only on specific ports (like
Examples:
for a web server).
# Display a basic CPU and I/O report
iostat Examples:
# Display a report every 2 seconds, 5 tim # List all current firewall rules
es, in a more readable format (kilobytes) iptables -L
iostat -k 2 5 # Block all incoming traffic from the IP
address 192.168.1.100
iptables -A INPUT -s 192.168.1.100 -j DRO
P
Linux Commands @DevOpsWithUs Prashant Gohel 10
46. systemctl 48. crontab
• Description: The main tool for • Description: Manages cron jobs, which
controlling systemd, the system and are scheduled tasks that run
service manager in modern Linux automatically at specified times or
distributions. It’s used to start, stop, and intervals.
manage services. • Real-World Use Case: To schedule a
• Real-World Use Case: To start a web backup script to run every night at 2 AM,
server after a reboot, check the status of a or to run a cleanup script every hour.
database service, or enable a service to
start automatically at boot. Examples:
# Edit the current user's crontab file
Examples:
crontab -e
# Check the status of the Apache web serv # List the scheduled cron jobs for the cu
er service rrent user
systemctl status apache2 crontab -l
# Start the nginx service
systemctl start nginx 49. at
• Description: Schedules a command to be
47. journalctl run once at a specific time in the future.
• Description: Queries and displays logs • Real-World Use Case: To schedule a
from journald, the systemd logging system reboot for later in the evening, or
service. It provides a centralized and to run a resource-intensive command
structured way to view system logs. during off-peak hours.
• Real-World Use Case: To troubleshoot a
service that failed to start or to view all Examples:
system error messages from the last hour. # Schedule a command to run at 10:30 PM
at 10:30 PM
Examples: # (You will then be prompted to enter the
# View all logs for the sshd service command, e.g., /sbin/shutdown -r now)
journalctl -u sshd # List the user's pending jobs
# Follow the system log in real-time atq
journalctl -f
Networking Commands
50. ip addr 51. ip route
• Description: A specific command within • Description: A specific command within
the ip utility to show and manage the ip utility to show and manage the
network addresses on devices. kernel routing table.
• Real-World Use Case: The primary way • Real-World Use Case: To check the
to find your machine’s IP address on default gateway (your router’s IP) that
modern Linux systems. your system uses to connect to the
internet.
Examples:
Examples:
# Show addresses for all network interfac
es # Show the main routing table
ip addr ip route
# Show the address for a specific interfa # Add a new route to a specific network v
ce, like eth0 ia a gateway
ip addr show eth0
Linux Commands @DevOpsWithUs Prashant Gohel 11
ip route add 192.168.5.0/24 via 192.168.1 Examples:
.1
# Perform a basic DNS lookup for example.
52. traceroute com
• Description: Traces the network path dig example.com
# Get only the MX (mail) records for a do
(the “hops”) that packets take to reach a
main
remote host. dig mx google.com
• Real-World Use Case: To diagnose
network slowdowns by seeing where in 55. ssh
the path the connection is getting delayed • Description: Securely connects to a
or failing. remote computer over a network. All
traffic is encrypted. ssh stands for “Secure
Examples:
Shell”.
# Trace the route to google.com • Real-World Use Case: The standard way
traceroute google.com for system administrators to log in to and
# Use TCP packets for the trace instead o manage remote servers.
f the default UDP
traceroute -T example.com Examples:
53. nslookup # Connect to a remote server with the use
• Description: A tool for querying Domain rname "admin"
ssh admin@192.168.1.100
Name System (DNS) servers to find the IP
# Connect to a server on a specific port
address associated with a domain name, (e.g., 2222)
or vice versa. ssh -p 2222 user@remote.server.com
• Real-World Use Case: To check if a
domain name is resolving to the correct 56. nmap
IP address. • Description: A powerful network
scanning tool used for network discovery
Examples:
and security auditing. It can discover
# Find the IP address for google.com hosts and services on a network.
nslookup google.com • Real-World Use Case: For security
# Find the domain name for a specific IP professionals to scan a network for open
address (reverse DNS lookup) ports and potential vulnerabilities.
nslookup 8.8.8.8
Examples:
54. dig
• Description: A more advanced and # Scan a host to see which common ports a
re open
flexible tool for querying DNS servers
nmap scanme.nmap.org
than nslookup. It provides more detailed # Perform an aggressive scan to detect OS
information. versions and services
• Real-World Use Case: For network nmap -A 192.168.1.1
administrators to get detailed DNS
information, like mail exchange (MX) 57. netcat (nc)
records for a domain. • Description: A versatile networking
utility for reading from and writing to
network connections using TCP or UDP.
• Real-World Use Case: For testing
network services, transferring files, or
creating simple client/server
applications.
Linux Commands @DevOpsWithUs Prashant Gohel 12
Examples: nc -zv google.com 80
# Create a simple chat server on port 123
# Check if a port is open on a remote hos 4
t nc -l 1234
File Management & Search
58. locate Examples:
• Description: Finds files quickly by # Display the directory tree starting fro
searching a pre-built database of all files m the current location
on the system. It’s much faster than find tree
but may not have the most up-to-date # Display the tree up to 2 levels deep
information. tree -L 2
• Real-World Use Case: When you need to
find a file instantly and you know its 61. file
name (or part of it). • Description: Determines the type of a file
by examining its contents (not just its
Examples: extension).
# Find any file or directory containing t • Real-World Use Case: To figure out what
he word "bashrc" a file is when it has no extension or an
locate bashrc incorrect one.
# Update the locate database (requires ad
min privileges) Examples:
sudo updated # Determine the type of a file named "mys
tery_file"
59. stat
file mystery_file
• Description: Displays detailed # Check the type of an image file
information about a file or filesystem, file logo.png
such as size, permissions,
access/modification times, and inode 62. basename
number. • Description: Extracts the filename from a
• Real-World Use Case: To see exactly given path, stripping away the directory
when a file was last accessed or modified, part.
beyond the basic information ls -l • Real-World Use Case: In shell scripts, to
provides. get just the name of a file when you have
its full path.
Examples:
Examples:
# Display detailed status information for
a file # Get the filename from a full path
stat my_document.txt basename /home/user/documents/report.txt
# Display information about a directory # Remove a specific suffix from the filen
stat /home/user ame
basename /home/user/images/photo.jpg .jpg
60. tree
• Description: Lists the contents of
directories in a tree-like format, making it
easy to visualize the directory structure.
• Real-World Use Case: To get a clear
overview of a project’s directory and file
structure.
Linux Commands @DevOpsWithUs Prashant Gohel 13
63. dirname Examples:
• Description: Extracts the directory part # Get the directory path from a full file
from a given path, stripping away the path
filename. dirname /home/user/documents/report.txt
• Real-World Use Case: In shell scripts, to # Get the parent directory of a directory
find the directory where a script is dirname /var/log/apache2/
located.
System Monitoring
64. vmstat which is useful when you can’t unmount a
• Description: Reports information about disk because it’s “busy”.
processes, memory, paging, block IO,
Examples:
traps, and CPU activity. vmstat stands for
“virtual memory statistics”. # List all processes that have a specific
• Real-World Use Case: For a quick file open
overview of system performance, lsof /var/log/syslog
including memory usage and CPU load. # Find out which process is using TCP por
t 80
Examples: lsof -i :80
# Run vmstat and provide a report every 2 67. dmesg
seconds • Description: Prints the message buffer of
vmstat 2 the kernel. These messages are generated
# Show statistics in a more readable form
by device drivers or the kernel itself.
at (megabytes)
vmstat -S M • Real-World Use Case: To troubleshoot
hardware issues, for example, to see if a
65. htop USB drive was recognized when you
• Description: An interactive process plugged it in.
viewer and system monitor. It’s a more
Examples:
user-friendly and colorful alternative to
top. # View all kernel messages
• Real-World Use Case: The preferred tool dmesg
for many to interactively manage ```bash
processes (e.g., kill, change priority) and # View the messages and pipe them to 'les
s' for easy scrolling
monitor system resources.
dmesg | less
Examples:
68. uptime
# Start the htop process viewer • Description: Shows how long the system
htop has been running since the last boot, the
# Show only processes for a specific user number of logged-in users, and the
htop -u www-data
system load average.
66. lsof • Real-World Use Case: To quickly check
• Description: Lists all files that are the stability of a server (a long uptime is
currently opened by processes. lsof often a good sign) and its current load.
stands for “list open files”.
• Real-World Use Case: To find out which
process is using a specific file or port,
Linux Commands @DevOpsWithUs Prashant Gohel 14
Examples: • Real-World Use Case: To find out which
process is causing heavy disk reads or
# Display the system uptime and load
writes, which can slow down the system.
uptime
# Get a "pretty" formatted output Examples:
uptime -p
# Start iotop (requires root privileges)
69. iotop sudo iotop
• Description: A top-like utility for # Show only processes that are actually d
monitoring real-time disk I/O usage by oing I/O
processes. sudo iotop -o
Package Management
70. apt 72. snap
• Description: The primary package • Description: A universal package
manager for Debian-based Linux management system that allows you to
distributions like Ubuntu. Used to install, install self-contained applications
update, and remove software. (“snaps”) that work across different Linux
• Real-World Use Case: To install a new distributions.
application like a web browser or to keep • Real-World Use Case: To install an
your system’s software up-to-date. application that might not be available in
your distribution’s default repositories,
Examples: like Spotify or Slack.
# Update the list of available packages
Examples:
sudo apt update
# Install the package named "vlc" # Find a snap package
sudo apt install vlc snap find spotify
# Install the spotify snap
71. yum/dnf sudo snap install spotify
• Description: The package manager for
RHEL-based distributions like CentOS and 73. rpm
Fedora. dnf is the modern replacement • Description: A command-line tool for
for yum. managing .rpm packages, which are used
• Real-World Use Case: To install or by Red Hat-based systems. It can install,
update software on a Red Hat-based query, and verify packages.
server. • Real-World Use Case: To install a piece
of software that was downloaded as an
Examples: .rpm file directly from a vendor’s website.
# Install the httpd package using dnf
Examples:
sudo dnf install httpd
# Check for system updates using yum # Install an rpm package file
sudo yum check-update sudo rpm -ivh some-package.rpm
# Query to see if a package is installed
rpm -q httpd
Linux Commands @DevOpsWithUs Prashant Gohel 15
Disk & File System
74. mount/umount th Windows)
• Description: mount attaches a storage sudo mkfs.vfat /dev/sdc1
device or filesystem to a directory, 77. blkid
making it accessible. umount detaches it.
• Description: Displays information about
• Real-World Use Case: To access the files available block devices (like hard drives),
on a USB drive after plugging it in, or to including their universally unique
safely remove it. identifiers (UUIDs) and filesystem types.
Examples: • Real-World Use Case: To find the UUID
of a partition, which is the recommended
# Mount a USB drive (e.g., /dev/sdb1) to way to identify it in the /etc/fstab file
the /mnt/usb directory
for auto-mounting.
sudo mount /dev/sdb1 /mnt/usb
# Unmount the USB drive Examples:
sudo umount /mnt/usb
# List all block devices and their attrib
75. fsck utes
• Description: Checks and repairs sudo blkid
inconsistencies in a Linux filesystem. fsck # Get information for a specific device
stands for “file system check”. sudo blkid /dev/sda1
• Real-World Use Case: To fix filesystem 78. lsblk
errors that can occur after an improper • Description: Lists information about all
shutdown or system crash. available or the specified block devices in
Examples: a tree-like format.
• Real-World Use Case: To get a clear
# Check the filesystem on a specific part overview of your disks and partitions and
ition (run on an unmounted partition)
how they are mounted.
sudo fsck /dev/sda1
# Automatically repair any errors found w Examples:
ithout prompting
sudo fsck -y /dev/sdb1 # List all block devices
lsblk
76. mkfs # Show more information, including filesy
• Description: Creates a new filesystem stem type and UUID
(like ext4, XFS, etc.) on a storage device or lsblk -f
partition. mkfs stands for “make
79. parted
filesystem”.
• Description: A powerful tool for creating
• Real-World Use Case: To format a new
and manipulating disk partition tables.
hard drive or USB stick so that it can be
used by the operating system. • Real-World Use Case: To partition a new
hard drive before creating filesystems on
Examples: it.
# Create an ext4 filesystem on a partitio Examples:
n
sudo mkfs.ext4 /dev/sdb1 # List the partitions on a specific disk
# Create a vfat filesystem (compatible wi sudo parted /dev/sda print
# Start parted in interactive mode for a
Linux Commands @DevOpsWithUs Prashant Gohel 16
disk
sudo parted /dev/sdb
Scripting and Automation
80. bash Examples:
• Description: The Bourne Again Shell, a # (cron is a service, typically managed w
command-line interpreter and a powerful ith systemctl)
scripting language. It’s the default shell # Check if the cron service is running
for most Linux distributions. systemctl status cron
• Real-World Use Case: Writing scripts to # View cron-related log messages
automate repetitive tasks, like backups, grep CRON /var/log/syslog
system updates, or processing files.
83. alias
Examples: • Description: Creates a shortcut or an
alternative name for a command or a
# Start a new bash shell
bash sequence of commands.
# Execute a shell script • Real-World Use Case: To create short,
bash my_script.sh easy-to-remember commands for long,
complex ones you use frequently.
81. sh
• Description: The original Bourne shell, a Examples:
simpler command interpreter. On modern # Create an alias 'll' for the 'ls -la' c
systems, /bin/sh is often a symbolic link ommand
to a more feature-rich shell like bash or alias ll='ls -la'
dash. # Create an alias to quickly update your
• Real-World Use Case: Writing scripts system (for Debian/Ubuntu)
that need to be highly portable and work alias update='sudo apt update && sudo apt
upgrade -y'
on older or more minimal systems.
Examples: 84. source
• Description: Executes commands from a
# Execute a script using the sh interpret file in the current shell session. This is
er useful for loading environment variables
sh portable_script.sh
or functions.
# Start an interactive sh shell
sh • Real-World Use Case: To apply changes
to your .bashrc file immediately without
82. cron having to log out and log back in.
• Description: The system daemon that
Examples:
executes scheduled commands. crontab
is the command used to manage the # Apply changes made to the .bashrc file
schedule. source ~/.bashrc
• Real-World Use Case: The underlying # A dot (.) is a shortcut for the source
service that runs the backup script you command
. ~/.bashrc
scheduled with crontab every night.
Linux Commands @DevOpsWithUs Prashant Gohel 17
Development and Debugging
85. gcc Examples:
• Description: The GNU Compiler # Trace the system calls of a program
Collection, a standard compiler for the C, strace ./my_program
C++, and Objective-C programming # Attach to a running process (with PID 1
languages. 234) and trace its calls
• Real-World Use Case: To compile source sudo strace -p 1234
code written in C into an executable
program. 88. gdb
• Description: The GNU Debugger, a
Examples: powerful tool that allows you to see what
# Compile a simple C program and create a is going on inside another program while
n executable named "hello" it executes.
gcc hello.c -o hello • Real-World Use Case: To step through
# Compile a program and include debugging code line-by-line, inspect variables, and
information find the exact location of a bug in a C or
gcc -g my_program.c -o my_program C++ program.
86. make Examples:
• Description: A build automation tool that
# Start debugging an executable (must be
automatically builds executable programs compiled with -g flag)
and libraries from source code by reading gdb ./my_program
a file called a Makefile. # (Inside gdb) Set a breakpoint at the ma
• Real-World Use Case: To simplify the in function and then run the program
compilation of large projects with many (gdb) break main
source files, by only recompiling files that (gdb) run
have changed.
89. git
Examples: • Description: A distributed version
control system for tracking changes in
# Run the default build process defined i
n the Makefile
source code during software
make development.
# Run a specific target, like "clean", to • Real-World Use Case: The standard tool
remove compiled files for managing codebases, collaborating
make clean with other developers, and tracking
project history.
87. strace
• Description: A debugging tool that traces Examples:
system calls (interactions between a # Clone a remote repository to your local
program and the Linux kernel) and machine
signals. git clone [https://github.com/user/repo.g
• Real-World Use Case: To figure out why it](https://github.com/user/repo.git)
a program is crashing or behaving # Check the status of your local changes
unexpectedly, by seeing which files it’s git status
trying to open or what system resources
it’s accessing.
Linux Commands @DevOpsWithUs Prashant Gohel 18
90. vim/nano Examples:
• Description: vim is a highly configurable # Open a file with vim
and powerful text editor, while nano is a vim /etc/hosts
simpler, more user-friendly text editor. # Open a file with nano
• Real-World Use Case: For editing nano ~/.bashrc
configuration files, writing scripts, or
coding directly in the terminal.
Other Useful Commands
91. whoami # Display the current hostname
• Description: Prints the username of the hostname
# Display the IP address for the hostname
user who is currently logged in.
hostname -i
• Real-World Use Case: To confirm which
user account you are operating under, 94. env
especially after using su or sudo. • Description: Displays all the
Examples: environment variables for the current
shell session.
# Display the current user's username • Real-World Use Case: To check the value
whoami of a specific variable like PATH to
# Use in a script to log which user ran i troubleshoot why a command isn’t found.
t
echo "Script was run by: $(whoami)" >> sc Examples:
ript.log
# List all environment variables
92. users env
• Description: Displays a list of usernames # Use with grep to find a specific variab
of all users currently logged in to the le
env | grep PATH
system.
• Real-World Use Case: To quickly see
who else is on a multi-user server.
95. export
Examples: • Description: Sets an environment
# List logged-in users variable, making it available to all child
users processes started from that shell.
# Pipe the output to wc to count the numb • Real-World Use Case: To set a
er of logged-in users temporary API key or configuration
users | wc -w setting for a script you are about to run.
Examples:
93. hostname # Create a new environment variable
• Description: Displays or sets the export MY_VARIABLE="some_value"
system’s host name. # Add a new directory to the PATH variabl
e
• Real-World Use Case: To quickly identify
export PATH=$PATH:/path/to/my/bin
which machine you are working on when
connected to multiple servers.
Examples:
Linux Commands @DevOpsWithUs Prashant Gohel 19
96. date 99. history
• Description: Displays or sets the system • Description: Displays a list of the
date and time. commands you have previously run in the
• Real-World Use Case: To check the current shell session.
current time on a server or to format the • Real-World Use Case: To find and re-run
date for use in a script’s log file name. a complex command you used earlier
without having to type it all out again.
Examples:
Examples:
# Display the current date and time
date # Display the command history
# Format the date to YYYY-MM-DD for a fil history
ename # Execute the 100th command from the hist
backup_filename="backup-$(date +%F).tar.g ory list
z" !100
echo $backup_filename
100. uname
97. cal • Description: Prints basic information
• Description: Displays a simple calendar about the system’s kernel and hardware
in the terminal. architecture. uname stands for “unix
• Real-World Use Case: A quick and easy name”.
way to check the calendar without leaving • Real-World Use Case: To quickly find out
the command line. if you’re on a 32-bit or 64-bit system, or
to see the kernel version.
Examples:
# Display the calendar for the current mo Examples:
nth # Print all available system information
cal uname -a
# Display the calendar for the entire yea # Print just the kernel release version
r 2025 uname -r
cal 2025
98. man
• Description: Displays the online manual
page (man page) for a specific command.
This is the primary way to get help in
Linux.
• Real-World Use Case: When you can’t
remember the options for a command like
tar or find, the man page has all the
details.
Examples:
# Show the manual page for the 'ls' comma
nd
man ls
# Show the manual for the crontab file fo
rmat (section 5)
man 5 crontab
Linux Commands @DevOpsWithUs Prashant Gohel 20