Master essential Linux commands with our comprehensive cheat sheet. Includes file operations, system monitoring, networking, and real-world examples for developers and system administrators.
๐ File and Directory Operations
Basic Navigation
# List directory contents
ls # Basic listing
ls -la # Detailed listing with hidden files
ls -lh # Human-readable file sizes
Real-world use case: Use ls -la
when troubleshooting permission issues or looking for hidden configuration files.
Pro tip: Add alias ll='ls -la'
to your .bashrc
for quick access.
Directory Navigation
# Change directories
cd /home/user # Go to specific directory
cd .. # Go up one level
cd ~ # Go to home directory
cd - # Go to previous directory
Example output:
$ pwd
/home/user/documents
$ cd ..
$ pwd
/home/user
File Operations
# Copy files and directories
cp file.txt backup.txt # Copy file
cp -r folder/ backup_folder/ # Copy directory recursively
cp *.txt /backup/ # Copy all .txt files
# Move/rename files
mv oldname.txt newname.txt # Rename file
mv file.txt /new/location/ # Move file
# Remove files and directories
rm file.txt # Remove file
rm -rf directory/ # Remove directory and contents (be careful!)
Real-world use case: Use cp -r
when backing up configuration directories before making changes.
Pro tip: Always use rm -i
for interactive deletion to avoid accidents.
๐ File Content and Search
Viewing File Content
# Display file contents
cat file.txt # Show entire file
head -n 10 file.txt # Show first 10 lines
tail -n 10 file.txt # Show last 10 lines
tail -f /var/log/syslog # Follow log file in real-time
Real-world use case: Use tail -f
to monitor log files during troubleshooting.
Text Search
# Search within files
grep "error" /var/log/syslog # Search for "error" in log file
grep -r "TODO" /home/user/code/ # Recursive search in directory
grep -i "warning" file.txt # Case-insensitive search
grep -n "function" script.py # Show line numbers
Example output:
$ grep -n "error" app.log
23:ERROR: Database connection failed
45:ERROR: Invalid user credentials
File Search
# Find files and directories
find /home -name "*.pdf" # Find PDF files
find . -type f -size +100M # Find large files (>100MB)
find /var/log -mtime -7 # Find files modified in last 7 days
Pro tip: Use find
with -exec
to perform actions on found files: find . -name "*.tmp" -exec rm {} \;
๐ System Information
System Status
# Check system resources
top # Real-time process viewer
htop # Enhanced process viewer (if installed)
ps aux # List all running processes
df -h # Disk usage by filesystem
du -sh * # Directory sizes in current location
Example output:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 15G 4.2G 79% /
/dev/sda2 100G 45G 50G 48% /home
Memory and CPU
# Memory information
free -h # Memory usage in human-readable format
cat /proc/meminfo # Detailed memory information
# CPU information
lscpu # CPU architecture details
nproc # Number of processing units
Real-world use case: Use free -h
to check if your system is running low on memory before starting resource-intensive tasks.
๐ Permissions and Ownership
File Permissions
# Change permissions
chmod 755 script.sh # Make script executable
chmod 644 document.txt # Standard file permissions
chmod -R 755 /var/www/ # Recursive permission change
# Change ownership
chown user:group file.txt # Change owner and group
chown -R www-data:www-data /var/www/ # Recursive ownership change
Permission Quick Reference:
- 755: Owner (rwx), Group (r-x), Others (r-x)
- 644: Owner (rw-), Group (r–), Others (r–)
- 600: Owner (rw-), Group (—), Others (—)
Pro tip: Use chmod +x filename
to make files executable without remembering numbers.
๐ Network Operations
Network Information
# Check network status
ping google.com # Test connectivity
wget https://example.com/file.zip # Download file
curl -I https://example.com # Get HTTP headers
netstat -tuln # Show listening ports
Example output:
$ ping -c 3 google.com
PING google.com (172.217.164.46): 56 data bytes
64 bytes from 172.217.164.46: icmp_seq=0 ttl=55 time=12.345 ms
64 bytes from 172.217.164.46: icmp_seq=1 ttl=55 time=11.234 ms
64 bytes from 172.217.164.46: icmp_seq=2 ttl=55 time=13.456 ms
SSH and Remote Access
# SSH connections
ssh user@hostname # Connect to remote server
ssh -p 2222 user@hostname # Connect using custom port
scp file.txt user@remote:/path/ # Copy file to remote server
Real-world use case: Use SSH key authentication for secure, password-less server access.
๐ฆ Package Management
APT (Ubuntu/Debian)
# Package operations
sudo apt update # Update package list
sudo apt upgrade # Upgrade installed packages
sudo apt install package-name # Install package
sudo apt remove package-name # Remove package
apt search keyword # Search for packages
YUM/DNF (Red Hat/CentOS/Fedora)
# Package operations
sudo yum update # Update packages (CentOS/RHEL)
sudo dnf install package-name # Install package (Fedora)
sudo dnf remove package-name # Remove package
Pro tip: Always run sudo apt update
before installing new packages to ensure you get the latest versions.
๐ง Process Management
Managing Processes
# Process control
ps aux | grep process-name # Find specific process
kill 1234 # Kill process by PID
killall firefox # Kill all instances of program
nohup command & # Run command in background
jobs # List background jobs
Real-world use case: Use nohup
when running long processes over SSH to prevent termination when connection drops.
Process Monitoring
# System monitoring
watch -n 1 'ps aux | head' # Monitor processes every second
pgrep -f "python script.py" # Find process ID by name
๐พ Archive and Compression
TAR Archives
# Create and extract archives
tar -czf backup.tar.gz /home/user/ # Create compressed archive
tar -xzf backup.tar.gz # Extract compressed archive
tar -tzf backup.tar.gz # List archive contents
File Extension Guide:
.tar.gz
or.tgz
: Gzip compressed tar archive.tar.bz2
: Bzip2 compressed tar archive.zip
: ZIP archive
ZIP Files
# ZIP operations
zip -r backup.zip /home/user/ # Create ZIP archive
unzip backup.zip # Extract ZIP archive
unzip -l backup.zip # List ZIP contents
๐ Text Processing
Advanced Text Operations
# Text manipulation
sort file.txt # Sort lines alphabetically
uniq file.txt # Remove duplicate lines
wc -l file.txt # Count lines
sed 's/old/new/g' file.txt # Replace text
awk '{print $1}' file.txt # Print first column
Real-world use case: Use sort | uniq
to find unique entries in log files.
Text Editors
# Command-line editors
nano file.txt # User-friendly editor
vim file.txt # Advanced editor
emacs file.txt # Another advanced editor
๐ System Monitoring
Log Analysis
# Log file management
tail -f /var/log/syslog # Follow system log
grep "ERROR" /var/log/apache2/error.log # Find errors in Apache log
journalctl -u nginx.service # View service logs (systemd)
Performance Monitoring
# System performance
iostat -x 1 # Monitor disk I/O
vmstat 1 # Monitor virtual memory
sar -u 1 5 # Monitor CPU usage
๐ Quick Pro Tips
Command Combinations
# Powerful command combinations
history | grep "git" # Find git commands in history
ps aux | grep python | awk '{print $2}' | xargs kill # Kill all Python processes
find . -name "*.log" -exec grep -l "ERROR" {} \; # Find log files with errors
Useful Aliases
Add these to your ~/.bashrc
:
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'
alias grep='grep --color=auto'
alias ..='cd ..'
alias ...='cd ../..'
Keyboard Shortcuts
Ctrl+C
: Kill current processCtrl+Z
: Suspend processCtrl+A
: Go to beginning of lineCtrl+E
: Go to end of lineCtrl+R
: Search command historyTab
: Auto-complete commands/filenames
๐ฏ Common Use Cases
Web Development
# Start local web server
python3 -m http.server 8000 # Python 3
python -m SimpleHTTPServer 8000 # Python 2
# Check port usage
lsof -i :8000 # See what's using port 8000
System Administration
# Check system health
uptime # System uptime and load
who # Logged in users
last # Login history
File Backup
# Backup strategies
rsync -avz /source/ /backup/ # Sync directories
cp -au /source/* /backup/ # Copy only newer files
Remember: Practice makes perfect! Try these commands in a safe environment first, and always backup important data before making system changes.
Safety First: Commands like rm -rf
can be destructive. Always double-check paths and use ls
to verify before deleting.