The Ultimate Guide to Common Linux Commands
Master the essential CLI commands that every systems engineer needs for efficient server management, from basic navigation to advanced text processing and network diagnostics.
Command-line proficiency separates effective infrastructure engineers from those who struggle with basic server operations. Whether you are managing a single development server or orchestrating hundreds of production instances, these commands form the foundation of daily operations.
1. Navigating the File System
ls - List Directory Contents
The ls command lists files and directories within the current directory.
Usage:
ls
Examples:
ls -l # Detailed listing with permissions, sizes, and modification dates
ls -a # List all files including hidden files
ls -lh # Human-readable file sizes
cd - Change Directory
The cd command is used to change the current directory.
Usage:
cd [directory]
Examples:
cd /home/user/Documents # Change to Documents directory
cd .. # Move up one directory level
cd ~ # Change to the home directory
pwd - Print Working Directory
The pwd command displays the current directory's full path.
Usage:
pwd
Example:
pwd # Outputs something like /home/user/Documents
2. File Management
cp - Copy Files and Directories
The cp command copies files or directories from one location to another.
Usage:
cp [source] [destination]
Examples:
cp file.txt /home/user/backup/ # Copy file.txt to the backup directory
cp -r /home/user/dir1 /home/user/dir2 # Recursively copy dir1 to dir2
mv - Move/Rename Files and Directories
The mv command moves files or directories to a new location or renames them.
Usage:
mv [source] [destination]
Examples:
mv file.txt /home/user/backup/ # Move file.txt to the backup directory
mv oldname.txt newname.txt # Rename oldname.txt to newname.txt
rm - Remove Files and Directories
The rm command deletes files or directories.
Usage:
rm [file]
Examples:
rm file.txt # Remove file.txt
rm -r /home/user/old_directory # Recursively remove old_directory
rm -i file.txt # Prompt before every removal
touch - Create Empty Files
The touch command creates an empty file or updates the timestamp of an existing file.
Usage:
touch [file]
Example:
touch newfile.txt # Create an empty file named newfile.txt
mkdir - Create Directories
The mkdir command creates new directories.
Usage:
mkdir [directory]
Examples:
mkdir new_directory # Create a directory named new_directory
mkdir -p dir1/dir2 # Create a directory tree with dir1 and dir2
rmdir - Remove Empty Directories
The rmdir command removes empty directories.
Usage:
rmdir [directory]
Example:
rmdir old_directory # Remove an empty directory named old_directory
3. File Viewing and Editing
cat - Concatenate and Display Files
The cat command displays the contents of a file or concatenates multiple files.
Usage:
cat [file]
Examples:
cat file.txt # Display the contents of file.txt
cat file1.txt file2.txt > combined.txt # Concatenate file1.txt and file2.txt into combined.txt
less - View File Contents Page by Page
The less command allows you to view file contents one page at a time.
Usage:
less [file]
Example:
less longfile.txt # View the contents of longfile.txt page by page
head - View the Beginning of a File
The head command displays the first few lines of a file.
Usage:
head [file]
Examples:
head file.txt # Display the first 10 lines of file.txt
head -n 20 file.txt # Display the first 20 lines of file.txt
tail - View the End of a File
The tail command displays the last few lines of a file.
Usage:
tail [file]
Examples:
tail file.txt # Display the last 10 lines of file.txt
tail -n 20 file.txt # Display the last 20 lines of file.txt
tail -f log.txt # Continuously monitor log.txt for new entries
4. System Information
uname - Display System Information
The uname command displays basic information about the system.
Usage:
uname [options]
Examples:
uname -a # Display all system information
uname -r # Display the kernel version
top - Display System Processes
The top command displays a real-time view of system processes.
Usage:
top
Example:
top # Launch the top utility to view system processes and resource usage
df - Disk Space Usage
The df command reports the amount of disk space used and available on filesystems.
Usage:
df [options]
Examples:
df -h # Display disk space usage in human-readable format
df -T # Display filesystem type
du - Directory Space Usage
The du command estimates file and directory space usage.
Usage:
du [options] [directory]
Examples:
du -h # Display space usage in human-readable format
du -sh directory # Summarize and display total space used by directory
free - Memory Usage
The free command displays the amount of free and used memory in the system.
Usage:
free [options]
Example:
free -h # Display memory usage in human-readable format
5. Managing Processes
ps - Display Process Information
The ps command provides information about active processes.
Usage:
ps [options]
Examples:
ps -e # Display all processes
ps aux # Detailed view of all processes
kill - Terminate Processes
The kill command terminates processes by their process ID (PID).
Usage:
kill [PID]
Examples:
kill 1234 # Terminate the process with PID 1234
kill -9 1234 # Forcefully terminate the process with PID 1234
pkill - Terminate Processes by Name
The pkill command terminates processes based on their name or other attributes.
Usage:
pkill [process_name]
Examples:
pkill firefox # Terminate all instances of Firefox
pkill -u username # Terminate all processes owned by the specified user
bg and fg - Background and Foreground Processes
The bg and fg commands are used to manage background and foreground processes.
Usage:
bg [job_spec]
fg [job_spec]
Examples:
ctrl+z # Suspend the current foreground process
bg # Resume the suspended process in the background
fg # Bring the most recent background process to the foreground
6. Networking
ping - Test Network Connectivity
The ping command checks the network connectivity to a host.
Usage:
ping [host]
Example:
ping google.com # Send ICMP ECHO_REQUEST packets to google.com
ifconfig - Configure Network Interfaces
The ifconfig command displays or configures network interfaces. Note: ifconfig is deprecated and replaced by ip.
Usage:
ifconfig [interface]
Example:
ifconfig eth0 # Display configuration of the eth0 interface
ip - Network Interface Management
The ip command is used for network interface management.
Usage:
ip [options] [command]
Examples:
ip addr show # Display IP addresses assigned to all interfaces
ip link set eth0 up # Bring the eth0 interface up
ip route show # Display the routing table
netstat - Network Statistics
The netstat command displays various network statistics.
Usage:
netstat [options]
Examples:
netstat -a # Display all active connections and listening ports
netstat -r # Display routing table
Advanced Linux Commands and Examples
For those who are already familiar with basic Linux commands, delving into more advanced commands can significantly enhance your efficiency and capabilities. Here are 10 advanced Linux commands with examples to help you take your Linux skills to the next level.
1. grep - Search Text Using Patterns
The grep command searches for patterns within files. It's a powerful tool for text processing and pattern matching.
Usage:
grep [options] pattern [file]
Examples:
grep "error" /var/log/syslog # Search for "error" in the syslog file
grep -r "TODO" ~/projects # Recursively search for "TODO" in the projects directory
grep -i "hello" file.txt # Case-insensitive search for "hello" in file.txt
2. find - Search for Files and Directories
The find command searches for files and directories in a directory hierarchy.
Usage:
find [path] [options] [expression]
Examples:
find /home/user -name "*.txt" # Find all .txt files in the home directory
find / -type d -name "backup" # Find directories named "backup" anywhere in the filesystem
find /var/log -mtime -7 # Find files in /var/log modified in the last 7 days
3. awk - Pattern Scanning and Processing Language
The awk command is used for pattern scanning and processing. It's a powerful programming language for working with text files.
Usage:
awk 'pattern {action}' [file]
Examples:
awk '{print $1}' file.txt # Print the first column of each line in file.txt
awk '/error/ {print $0}' /var/log/syslog # Print lines containing "error" in syslog
awk -F: '{print $1, $3}' /etc/passwd # Print the first and third fields (colon-separated) from /etc/passwd
4. sed - Stream Editor for Filtering and Transforming Text
The sed command is used to perform basic text transformations on an input stream (a file or input from a pipeline).
Usage:
sed [options] 'script' [file]
Examples:
sed 's/old/new/g' file.txt # Replace all occurrences of "old" with "new" in file.txt
sed -n '5,10p' file.txt # Print lines 5 to 10 from file.txt
sed '/^#/d' file.txt # Delete lines starting with a hash (#) in file.txt
5. rsync - Remote Sync
The rsync command is used to synchronize files and directories between two locations over a network or locally.
Usage:
rsync [options] source destination
Examples:
rsync -avz /home/user/docs/ remote:/backup/docs/ # Synchronize docs directory to remote server with compression
rsync -av --delete /source/ /destination/ # Synchronize and delete files not present in source
rsync -azP /home/user/backup user@remote:/backup # Synchronize with progress and partial transfers
6. tar - Archive Files
The tar command is used to create and extract archive files.
Usage:
tar [options] [archive-file] [file/directory]
Examples:
tar -cvf archive.tar /path/to/directory # Create an archive from a directory
tar -xvf archive.tar # Extract files from an archive
tar -czvf archive.tar.gz /path/to/directory # Create a compressed archive
tar -xzvf archive.tar.gz # Extract a compressed archive
7. curl - Transfer Data from or to a Server
The curl command is used to transfer data from or to a server using various protocols.
Usage:
curl [options] [URL]
Examples:
curl -O # Download a file from a URL
curl -u user:password # Download a file from an FTP server with authentication
curl -X POST -d "name=value" # Send a POST request with data
8. iptables - Configure Network Packet Filtering Rules
The iptables command is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel.
Usage:
iptables [options]
Examples:
iptables -L # List all current rules
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow incoming SSH connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow established connections
iptables -A INPUT -p tcp --dport 80 -j DROP # Drop all incoming HTTP connections
9. cron - Schedule Jobs
The cron command is used to schedule jobs to run at specific times or intervals.
Usage:
crontab [options]
Examples:
crontab -e # Edit the crontab file for the current user
crontab -l # List the current user's crontab
# Example cron job to run a script every day at midnight
0 0 * * * /path/to/script.sh
10. ssh - Secure Shell
The ssh command is used to securely connect to a remote machine over the network.
Usage:
ssh [options] [user@hostname]
Examples:
ssh user@remote-server.com # Connect to a remote server
ssh -i /path/to/private_key user@remote-server.com # Connect using a private key
ssh -L 8080:localhost:80 user@remote-server.com # Set up local port forwarding
Key Takeaways
- Master the basics first: Commands like
ls,cd,cp,mv, andrmform the foundation of all file system operations. Invest time in understanding their flags and options. - Use
tail -ffor real-time log monitoring: This is indispensable for debugging production issues and monitoring application behavior. - Prefer
ipoverifconfig: Theifconfigcommand is deprecated on modern distributions. Migrate your muscle memory toip addrandip link. - Combine
grep,awk, andsedfor powerful text processing: These three commands together can handle virtually any log parsing or data extraction task. - Automate repetitive tasks with
cron: Schedule backups, log rotation, and maintenance scripts to run automatically. - Use
rsyncfor reliable file transfers: Unlikescp,rsyncsupports incremental transfers and can resume interrupted operations. - Understand process management: Knowing how to use
ps,kill, and background/foreground job control is essential for managing long-running operations.
Written by Bar Tsveker
Senior CloudOps Engineer specializing in AWS, Terraform, and infrastructure automation.
Thanks for reading! Have questions or feedback?