59 most used Linux commands - with usage examples
This is a compilation of some of the most used commands in Linux. I find that if you already know 50% of these, you're doing quite well.
The list
- ls: List directory contents.
- Scenario 1: View files in a directory:
ls
- Scenario 2: Display detailed information:
ls -l
- Scenario 3: Display detailed information, including hidden files, in human readable format:
ls -lah
- Scenario 1: View files in a directory:
- cd: Change directory.
- Scenario 1: Move to the home directory:
cd ~
- Scenario 2: Navigate to a specific directory:
cd /path/to/directory
- Scenario 1: Move to the home directory:
- pwd: Print working directory.
- Scenario 1: View the current directory:
pwd
- Scenario 2: Use the absolute path in a script:
echo $(pwd)
- Scenario 1: View the current directory:
- cp: Copy files and directories.
- Scenario 1: Duplicate a file:
cp file1 file2
- Scenario 2: Copy a directory and its contents recursively:
cp -r dir1 dir2
- Scenario 1: Duplicate a file:
- mv: Move or rename files and directories.
- Scenario 1: Rename a file:
mv oldname.txt newname.txt
- Scenario 2: Move a file to another directory:
mv file1 /path/to/directory
- Scenario 1: Rename a file:
- rm: Remove files and directories.
- Scenario 1: Delete a file:
rm file
- Scenario 2: Remove a directory and its contents recursively:
rm -r directory
- Scenario 1: Delete a file:
- mkdir: Create directories.
- Scenario 1: Make a new directory:
mkdir new_directory
- Scenario 2: Create nested directories:
mkdir -p parent_directory/new_directory
- Scenario 1: Make a new directory:
- touch: Create an empty file.
- Scenario 1: Create a new file:
touch new_file.txt
- Scenario 2: Update the modification time of an existing file:
touch existing_file.txt
- Scenario 1: Create a new file:
- cat: Concatenate and display file content.
- Scenario 1: View file content:
cat file.txt
- Scenario 2: Display line numbers in the output:
cat -n file.txt
- Scenario 1: View file content:
- grep: Search for patterns in files.
- Scenario 1: Find occurrences of a word in a file:
grep "word" file.txt
- Scenario 2: Search recursively in directories for a specific pattern:
grep -r "pattern" /path/to/directory
- Scenario 3: Find text within files:
grep "pattern" file
- Scenario 4: Count occurrences of a pattern in multiple files:
grep -c "pattern" file1 file2
- Scenario 5: Search for a pattern in a file recursively:
grep -r "pattern" /path/to/directory
- Scenario 6: Display lines that do not match a pattern:
grep -v "pattern" file.txt
- Scenario 1: Find occurrences of a word in a file:
- chmod: Change file permissions.
- Scenario 1: Grant execute permission to a file:
chmod +x script.sh
- Scenario 2: Restrict permissions for a file:
chmod go-rwx file
- Scenario 1: Grant execute permission to a file:
- chown: Change file owner and group.
- Scenario 1: Change the owner of a file:
chown user:group file
- Scenario 2: Change the owner of a directory and its contents recursively:
chown -R user:group directory
- Scenario 1: Change the owner of a file:
- ps: Display currently running processes.
- Scenario 1: List all processes:
ps aux
- Scenario 2: Show processes of a specific user:
ps -u username
- Scenario 1: List all processes:
- kill: Terminate processes by ID or name.
- Scenario 1: Stop a process using its ID:
kill PID
- Scenario 2: Forcefully terminate a process:
kill -9 PID
- Scenario 1: Stop a process using its ID:
- tar: Archive files.
- Scenario 1: Create a compressed archive:
tar -czvf archive.tar.gz directory
- Scenario 2: Extract an archive to a specific directory:
tar -xzvf archive.tar.gz -C /path/to/destination
- Scenario 1: Create a compressed archive:
- gunzip: Decompress files.
- Scenario 1: Decompress a file:
gunzip file.gz
- Scenario 2: Decompress multiple files with a wildcard:
gunzip *.gz
- Scenario 3: Decompress multiple files:
gunzip file1.gz file2.gz
- Scenario 1: Decompress a file:
- wget: Retrieve files from the internet.
- Scenario 1: Download a file:
wget URL
- Scenario 2: Continue an interrupted download:
wget -c URL
- Scenario 1: Download a file:
- scp: Securely copy files over SSH.
- Scenario 1: Copy a file from a remote server to local machine:
scp user@remote:/path/to/file /local/path
- Scenario 2: Copy a directory from local to remote:
scp -r /local/directory user@remote:/remote/path
- Scenario 3: Copy a file from local to remote:
scp file.txt user@remote:/path
- Scenario 1: Copy a file from a remote server to local machine:
- ssh: Securely access remote servers.
- Scenario 1: Connect to a server using a specific private key:
ssh -i /path/to/private_key user@host
- Scenario 2: Establish an SSH tunnel for port forwarding:
ssh -L 8080:localhost:80 user@host
- Scenario 1: Connect to a server using a specific private key:
- ifconfig/ip: Display network interface information.
- Scenario 1: List all network interfaces:
ifconfig -a
- Scenario 2: Set an IP address for a specific interface:
sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0
- Scenario 1: List all network interfaces:
- netstat: Display network connections, routing tables, interface statistics.
- Scenario 1: Show all listening ports:
netstat -tulnp
- Scenario 2: Display statistics for a specific network protocol:
netstat -s
- Scenario 1: Show all listening ports:
- route: View and manipulate IP routing tables.
- Scenario 1: Add a new route to the routing table:
sudo route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.0.1
- Scenario 2: Delete a specific route:
sudo route del -net 192.168.1.0 netmask 255.255.255.0
- Scenario 1: Add a new route to the routing table:
- ping: Send ICMP echo requests to check connectivity.
- Scenario 1: Test network connectivity to a host:
ping google.com
- Scenario 2: Ping continuously and record the output to a file:
ping -i 2 google.com >> ping_log.txt
- Scenario 1: Test network connectivity to a host:
- traceroute/tracert: Display the route packets take to a network host.
- Scenario 1: Trace the path packets take:
traceroute google.com
- Scenario 2: Limit the number of hops:
traceroute -m 20 google.com
- Scenario 1: Trace the path packets take:
- nslookup/dig: Query DNS servers for domain information.
- Scenario 1: Retrieve DNS information:
nslookup domain.com
- Scenario 2: Perform a reverse DNS lookup:
nslookup 8.8.8.8
- Scenario 1: Retrieve DNS information:
- hostname: Display or set system hostname.
- Scenario 1: View the current hostname:
hostname
- Scenario 2: Change the hostname temporarily:
sudo hostname temporary_name
- Scenario 1: View the current hostname:
- uname: Display system information.
- Scenario 1: Check kernel version:
uname -r
- Scenario 2: Show system architecture:
uname -m
- Scenario 1: Show only the kernel name:
uname -s
- Scenario 1: Check kernel version:
- whoami: Display current username.
- Scenario 1: Identify the current logged-in user:
whoami
- Scenario 2: Use in a script to confirm executing user:
if [ "$(whoami)" != "root" ]; then echo "Please run as root"; fi
- Scenario 1: Identify the current logged-in user:
- df: Display disk space usage.
- Scenario 1: Show available space in human-readable format:
df -h
- Scenario 2: Display space usage of a specific filesystem:
df -T /dev/sda1
- Scenario 1: Show available space in human-readable format:
- du: Display directory space usage.
- Scenario 1: Show sizes of directories in the current location:
du -h
- Scenario 2: Display total size of a directory:
du -sh directory
- Scenario 1: Show sizes of directories in the current location:
- find: Search for files and directories.
- Scenario 1: Find files modified within the last 24 hours:
find / -mtime -1
- Scenario 2: Search for files owned by a specific user:
find / -user username
- Scenario 1: Find files modified within the last 24 hours:
- awk: Pattern scanning and text processing tool.
- Scenario 1: Print lines longer than a specific length:
awk 'length > 80' file
- Scenario 2: Extract and print specific fields from a CSV file:
awk -F',' '{print $1, $3}' file.csv
- Scenario 3: Print lines matching a specific field:
awk '$3 ~ /pattern/' file.txt
- Scenario 4: Calculate the sum of a specific column:
awk '{sum+=$2} END {print sum}' file.txt
- Scenario 1: Print lines longer than a specific length:
- sed: Stream editor for filtering and transforming text.
- Scenario 1: Replace text in multiple files:
sed -i 's/old/new/g' file1 file2
- Scenario 2: Insert text at the beginning of a file:
sed -i '1iThis is a header' file
- Scenario 3: Remove leading and trailing whitespaces in a file:
sed -i 's/^[ \t]*//;s/[ \t]*$//' file.txt
- Scenario 4: Replace a specific string in a file:
sed -i 's/old/new/g' file.txt
- Scenario 1: Replace text in multiple files:
- systemctl: Control system services.
- Scenario 1: Enable a service to start at boot:
sudo systemctl enable servicename
- Scenario 2: Check the status of a service:
systemctl status ssh
- Scenario 1: Enable a service to start at boot:
- journalctl: Query and display system logs.
- Scenario 1: Filter logs for a specific service:
journalctl -u servicename
- Scenario 2: Display logs since a specific time:
journalctl --since "2023-12-01 00:00:00"
- Scenario 1: Filter logs for a specific service:
- firewall-cmd/ufw: Configure firewall rules.
- Scenario 1: Allow all traffic from a specific IP address:
sudo ufw allow from 192.168.1.100
- Scenario 2: Deny all incoming traffic by default:
sudo ufw default deny incoming
- Scenario 1: Allow all traffic from a specific IP address:
- lsof: List open files and processes.
- Scenario 1: List processes using a specific file:
lsof /path/to/file
- Scenario 2: Show network-related processes:
lsof -i
- Scenario 1: List processes using a specific file:
- tcpdump: Network packet analyzer.
- Scenario 1: Capture packets on a specific interface:
sudo tcpdump -i eth0
- Scenario 2: Display HTTP traffic:
sudo tcpdump -i eth0 port 80
- Scenario 1: Capture packets on a specific interface:
- netcat (nc): Network utility for reading and writing data across network connections.
- Scenario 1: Create a simple TCP server:
nc -l -p 1234
- Scenario 2: Transfer files between systems:
nc -l -p 1234 < file.txt
- Scenario 1: Create a simple TCP server:
- ssh-keygen: Create SSH key pairs for secure authentication.
- Scenario 1: Generate an RSA key pair:
ssh-keygen -t rsa
- Scenario 2: Create a key pair with a specific filename:
ssh-keygen -t rsa -f /path/to/key
- Scenario 1: Generate an RSA key pair:
- ssh-copy-id: Copy SSH keys to a remote server.
- Scenario 1: Copy the public key to a remote host:
ssh-copy-id user@host
- Scenario 2: Use a specific port for copying the key:
ssh-copy-id -p port user@host
- Scenario 1: Copy the public key to a remote host:
- rsync: Remote file synchronization and transfer.
- Scenario 1: Synchronize files and directories between local and remote:
rsync -avz /source user@remote:/destination
- Scenario 2: Delete files at the destination that are not in the source:
rsync -av --delete /source user@remote:/destination
- Scenario 1: Synchronize files and directories between local and remote:
- tail: Display the last part of a file.
- Scenario 1: View the last 10 lines of a file:
tail file.txt
- Scenario 2: Continuously display new lines appended to a file:
tail -f log.txt
- Scenario 1: View the last 10 lines of a file:
- head: Display the beginning of a file.
- Scenario 1: View the first 10 lines of a file:
head file.txt
- Scenario 2: Display a specific number of lines from the start:
head -n 15 file.txt
- Scenario 1: View the first 10 lines of a file:
- more: View file content one page at a time.
- Scenario 1: Display content page by page:
more file.txt
- Scenario 2: Search for a specific pattern in a file:
more file.txt +/pattern
- Scenario 1: Display content page by page:
- less: View file content with backward navigation.
- Scenario 1: Scroll through file content:
less file.txt
- Scenario 2: Search for text and navigate through matches:
less +/pattern file.txt
- Scenario 1: Scroll through file content:
- echo: Display a line of text or a variable.
- Scenario 1: Print a message:
echo "Hello, World!"
- Scenario 2: Assign a value to a variable:
variable="value"; echo $variable
- Scenario 3: Output text to a file:
echo "Content" > file.txt
- Scenario 4: Append text to a file:
echo "More content" >> file.txt
- Scenario 1: Print a message:
- date: Display or set the system date and time.
- Scenario 1: Show the current date and time:
date
- Scenario 2: Display date and time in a specific format:
date +"%Y-%m-%d %H:%M:%S"
- Scenario 1: Show the current date and time:
- curl: Transfer data from or to a server.
- Scenario 1: Retrieve content from a URL:
curl URL
- Scenario 2: Send data via POST request:
curl -X POST -d "data" URL
- Scenario 1: Retrieve content from a URL:
- history: Display command history.
- Scenario 1: Show the list of recent commands:
history
- Scenario 2: Search for commands containing specific text:
history | grep "pattern"
- Scenario 1: Show the list of recent commands:
- which: Locate a command.
- Scenario 1: Find the path of an executable:
which command
- Scenario 2: Check if a command is available in the system:
which ls
- Scenario 1: Find the path of an executable:
- wc: Display line, word, and character count for a file.
- Scenario 1: Count lines in a file:
wc -l file.txt
- Scenario 2: Count words in a file:
wc -w file.txt
- Scenario 1: Count lines in a file:
- uniq: Report or omit repeated lines in a file.
- Scenario 1: Show unique lines in a sorted file:
sort file.txt | uniq
- Scenario 2: Display count of occurrences for unique lines:
sort file.txt | uniq -c
- Scenario 1: Show unique lines in a sorted file:
- sort: Sort lines of text files.
- Scenario 1: Sort lines in a file:
sort file.txt
- Scenario 2: Sort lines numerically:
sort -n file.txt
- Scenario 1: Sort lines in a file:
- alias: Create shortcuts for commands.
- Scenario 1: Create an alias for a command:
alias ll='ls -l'
- Scenario 2: List all defined aliases:
alias
- Scenario 1: Create an alias for a command:
- diff: Compare files line by line.
- Scenario 1: Compare two files and display differences:
diff file1.txt file2.txt
- Scenario 2: Show differences between two directories:
diff -r dir1/ dir2/
- Scenario 1: Compare two files and display differences:
- gzip: Compress files.
- Scenario 1: Compress a file:
gzip file.txt
- Scenario 2: Compress multiple files:
gzip file1 file2
- Scenario 3: Compress multiple files using wildcards:
gzip *.txt
- Scenario 1: Compress a file:
- date: Display or set the system date and time.
- Scenario 1: Set the system date and time:
sudo date MMDDhhmmYYYY
- Scenario 2: Display date and time in UTC:
date -u
- Scenario 1: Set the system date and time:
- uptime: Show how long the system has been running.
- Scenario 1: Display system uptime:
uptime
- Scenario 2: Show load average for the last 1, 5, and 15 minutes:
uptime -p
- Scenario 1: Display system uptime:
Conclusion
It's important to understand that nobody knows these Linux commands 100% by heart. For example, I sometimes forgot the usage find - a quick Google search is no big deal. Using them more often gives you tho aha! moments where you remembered it for sure. What's important is knowing the right tool for the job.