Whether you are managing servers, writing scripts, or just navigating your development environment, Linux command-line proficiency is an essential skill. This guide covers the most useful commands organized by task, with practical examples you can use immediately.
Key Takeaways
- 1Master file navigation with cd, ls, find, and locate commands
- 2Process text efficiently with grep, sed, awk, and pipes
- 3Manage permissions with chmod and chown using numeric or symbolic notation
- 4Monitor processes with ps, top, and control them with kill signals
- 5Use shell scripting to automate repetitive tasks
2File Operations
Create, copy, move, and delete files with these essential commands.
# Create files and directories
touch newfile.txt # Create empty file or update timestamp
mkdir newdir # Create directory
mkdir -p path/to/nested # Create nested directories
# Copy files
cp file1.txt file2.txt # Copy file
cp -r dir1/ dir2/ # Copy directory recursively
cp -i file1.txt file2.txt # Interactive (ask before overwrite)
cp -p file1.txt file2.txt # Preserve permissions and timestamps
# Move and rename
mv oldname.txt newname.txt # Rename file
mv file.txt /new/path/ # Move file
mv -i file.txt dest/ # Ask before overwrite
# Delete files
rm file.txt # Delete file
rm -r directory/ # Delete directory recursively
rm -f file.txt # Force delete (no confirmation)
rm -rf directory/ # Force delete directory (use carefully!)
rmdir emptydir # Delete empty directory only
# Links
ln -s /path/to/target linkname # Create symbolic link
ln target hardlink # Create hard link
readlink -f linkname # Show where symlink points
# File info
file document.pdf # Detect file type
stat file.txt # Detailed file information
du -sh directory/ # Directory size
du -h --max-depth=1 . # Size of subdirectoriesrm -rf is extremely dangerous, especially with wildcards or as root. Double-check paths before running. Consider using trash-cli instead for recoverable deletes.
Text Processing
Linux excels at text manipulation. These commands are essential for log analysis, data processing, and scripting.
# Viewing files
cat file.txt # Output entire file
less file.txt # Paginated view (q to quit)
head -n 20 file.txt # First 20 lines
tail -n 20 file.txt # Last 20 lines
tail -f /var/log/syslog # Follow file (live updates)
# Searching with grep
grep "error" log.txt # Find lines containing "error"
grep -i "error" log.txt # Case insensitive
grep -r "TODO" src/ # Recursive search in directory
grep -n "error" log.txt # Show line numbers
grep -v "debug" log.txt # Invert (lines NOT matching)
grep -c "error" log.txt # Count matches
grep -E "error|warning" log.txt # Extended regex (OR)
grep -A 3 -B 1 "error" log.txt # 3 lines after, 1 before match
# Alternative: ripgrep (rg) - faster for codebases
rg "TODO" --type py # Search only Python files# Sorting and deduplicating
sort file.txt # Sort lines alphabetically
sort -n numbers.txt # Sort numerically
sort -r file.txt # Reverse sort
sort -u file.txt # Sort and remove duplicates
uniq file.txt # Remove adjacent duplicates
sort file.txt | uniq -c # Count occurrences
# Cutting and extracting
cut -d',' -f1,3 data.csv # Extract columns 1 and 3
cut -c1-10 file.txt # Extract characters 1-10
awk '{print $1, $3}' file.txt # Print columns 1 and 3
awk -F',' '{print $2}' data.csv # CSV column 2
# Text transformation
tr 'a-z' 'A-Z' < file.txt # Convert to uppercase
tr -d ' ' < file.txt # Delete all spaces
sed 's/old/new/g' file.txt # Replace text (global)
sed -i 's/old/new/g' file.txt # Replace in-place
sed -n '10,20p' file.txt # Print lines 10-20
# Word and line counts
wc -l file.txt # Count lines
wc -w file.txt # Count words
wc -c file.txt # Count bytesCombine commands with pipes: cat log.txt | grep "error" | sort | uniq -c | sort -rn | head -10 shows top 10 most frequent errors.
4Permissions and Ownership
Linux file permissions control who can read, write, and execute files.
# View permissions
ls -l file.txt
# Output: -rw-r--r-- 1 user group 1234 Jan 25 10:00 file.txt
# Format: [type][owner][group][others]
# r=read(4), w=write(2), x=execute(1)
# Change permissions
chmod 755 script.sh # rwxr-xr-x (owner all, others read+execute)
chmod 644 file.txt # rw-r--r-- (owner read+write, others read)
chmod +x script.sh # Add execute permission
chmod -w file.txt # Remove write permission
chmod u+x,g+r file.txt # User add execute, group add read
chmod -R 755 directory/ # Recursive
# Common permission values
# 755 - Directories and executables
# 644 - Regular files
# 600 - Private files (only owner)
# 700 - Private directories
# Change ownership
chown user file.txt # Change owner
chown user:group file.txt # Change owner and group
chown -R user:group directory/ # Recursive
chgrp group file.txt # Change group only
# Special permissions
chmod u+s executable # SUID (run as owner)
chmod g+s directory/ # SGID (inherit group)
chmod +t /tmp # Sticky bit (only owner can delete)| Numeric | Symbolic | Description |
|---|---|---|
| 7 | rwx | Read, write, execute |
| 6 | rw- | Read, write |
| 5 | r-x | Read, execute |
| 4 | r-- | Read only |
| 0 | --- | No permissions |
Process Management
Monitor and control running processes on your system.
# View processes
ps aux # All processes (BSD style)
ps -ef # All processes (POSIX style)
ps aux | grep nginx # Find specific process
pgrep nginx # Get PID by name
top # Interactive process viewer
htop # Better interactive viewer (if installed)
# Process details
ps aux --sort=-%mem | head # Top memory consumers
ps aux --sort=-%cpu | head # Top CPU consumers
lsof -p 1234 # Files opened by PID 1234
lsof -i :8080 # Process using port 8080
# Background and foreground
command & # Run in background
jobs # List background jobs
fg %1 # Bring job 1 to foreground
bg %1 # Resume job 1 in background
Ctrl+Z # Suspend current process
nohup command & # Run immune to hangups
# Stop processes
kill 1234 # Send SIGTERM to PID 1234
kill -9 1234 # Send SIGKILL (force kill)
killall nginx # Kill all by name
pkill -f "python script" # Kill by pattern in command
timeout 10 command # Kill after 10 seconds# Common signals
kill -SIGTERM 1234 # or kill -15 (graceful shutdown)
kill -SIGKILL 1234 # or kill -9 (force kill, no cleanup)
kill -SIGHUP 1234 # or kill -1 (reload configuration)
kill -SIGSTOP 1234 # or kill -19 (pause process)
kill -SIGCONT 1234 # or kill -18 (resume process)
# List all signals
kill -lNetworking Commands
Diagnose network issues and manage connections from the command line.
# Network interfaces
ip addr # Show IP addresses
ip link # Show network interfaces
ifconfig # Legacy (deprecated)
hostname -I # Show IP addresses only
# DNS and connectivity
ping google.com # Test connectivity
ping -c 4 google.com # Ping 4 times then stop
traceroute google.com # Trace route to host
mtr google.com # Continuous traceroute
nslookup google.com # DNS lookup
dig google.com # Detailed DNS lookup
dig +short google.com # Just the IP
host google.com # Simple DNS lookup
# Ports and connections
netstat -tulpn # Listening ports (legacy)
ss -tulpn # Listening ports (modern)
ss -s # Socket statistics summary
lsof -i :80 # What's using port 80
# HTTP requests
curl https://api.example.com # GET request
curl -I https://api.example.com # Headers only
curl -X POST -d '{"key":"val"}' url # POST JSON
curl -o file.zip https://example.com/file # Download to file
wget https://example.com/file.zip # Download file
# File transfer
scp file.txt user@host:/path/ # Copy to remote
scp user@host:/path/file.txt ./ # Copy from remote
rsync -avz dir/ user@host:/path/ # Sync directory# SSH connections
ssh user@hostname # Connect to remote host
ssh -p 2222 user@hostname # Connect on custom port
ssh -i ~/.ssh/key.pem user@hostname # Use specific key
ssh -L 8080:localhost:80 user@host # Local port forward
ssh -R 8080:localhost:80 user@host # Remote port forward
ssh -D 1080 user@host # SOCKS proxy
# SSH keys
ssh-keygen -t ed25519 -C "email" # Generate new key
ssh-copy-id user@hostname # Copy key to server
ssh-add ~/.ssh/id_ed25519 # Add key to agent7Disk and Storage
Monitor disk usage and manage storage.
# Disk usage
df -h # Filesystem disk usage (human readable)
df -h / # Usage for specific mount
du -sh directory/ # Size of directory
du -h --max-depth=1 . # Sizes of subdirectories
ncdu /var # Interactive disk usage analyzer
# Find large files
find / -type f -size +500M 2>/dev/null # Files over 500MB
du -ah . | sort -rh | head -20 # Top 20 largest files/dirs
# Disk information
lsblk # List block devices
fdisk -l # Partition information (as root)
mount # Show mounted filesystems
cat /etc/fstab # Permanent mount configuration
# Mount and unmount
mount /dev/sdb1 /mnt/usb # Mount device
umount /mnt/usb # Unmount
mount -o remount,rw / # Remount with different options
# Check and repair
fsck /dev/sdb1 # Check filesystem (unmounted only!)# Clean up space
# Clear package cache
sudo apt clean # Debian/Ubuntu
sudo yum clean all # RHEL/CentOS
# Find and remove old logs
sudo journalctl --vacuum-time=7d # Keep only 7 days of logs
sudo journalctl --vacuum-size=500M # Limit log size
# Docker cleanup
docker system prune -a # Remove unused images, containers
# Clear temp files
sudo rm -rf /tmp/* # Clear temp
rm -rf ~/.cache/* # Clear user cache8System Information
Get information about your system\
# Operating system info
uname -a # Full system info
cat /etc/os-release # OS version
hostnamectl # Hostname and OS info
uptime # System uptime and load
# Hardware info
lscpu # CPU information
free -h # Memory usage
cat /proc/meminfo # Detailed memory info
lsblk # Block devices
lspci # PCI devices
lsusb # USB devices
# System load and resources
top # Process and resource monitor
htop # Better process monitor
vmstat 1 # Virtual memory stats (every 1 sec)
iostat 1 # IO statistics
sar # System activity report
# Logs
dmesg # Kernel messages
dmesg | tail # Recent kernel messages
journalctl # Systemd journal
journalctl -u nginx # Logs for specific service
journalctl -f # Follow logs (like tail -f)
tail -f /var/log/syslog # Traditional syslog# Service management (systemd)
systemctl status nginx # Check service status
systemctl start nginx # Start service
systemctl stop nginx # Stop service
systemctl restart nginx # Restart service
systemctl reload nginx # Reload config without restart
systemctl enable nginx # Start on boot
systemctl disable nginx # Don't start on boot
systemctl list-units --type=service --all # List all services
# Check what's listening
ss -tulpn # Listening ports with process infoShell Scripting Basics
Combine commands into reusable scripts for automation.
#!/bin/bash
# Shebang tells system which interpreter to use
# Variables
NAME="John"
echo "Hello, $NAME"
CURRENT_DIR=$(pwd) # Command substitution
FILES=$(ls -la)
# Conditionals
if [ -f "file.txt" ]; then
echo "File exists"
elif [ -d "directory" ]; then
echo "Directory exists"
else
echo "Not found"
fi
# Common test operators
# -f file : File exists and is regular file
# -d dir : Directory exists
# -z string : String is empty
# -n string : String is not empty
# -e path : Path exists (file or directory)
# $a -eq $b : Numbers are equal
# $a -ne $b : Numbers are not equal
# $a -gt $b : a greater than b
# $a -lt $b : a less than b
# Loops
for file in *.txt; do
echo "Processing $file"
done
for i in {1..10}; do
echo "Count: $i"
done
while read line; do
echo "$line"
done < file.txt#!/bin/bash
# Example: Backup script
BACKUP_DIR="/backup"
SOURCE_DIR="/var/www"
DATE=$(date +%Y%m%d_%H%M%S)
FILENAME="backup_$DATE.tar.gz"
# Create backup directory if not exists
mkdir -p "$BACKUP_DIR"
# Create compressed backup
tar -czf "$BACKUP_DIR/$FILENAME" "$SOURCE_DIR"
# Check if backup succeeded
if [ $? -eq 0 ]; then
echo "Backup created: $FILENAME"
# Keep only last 7 backups
ls -t "$BACKUP_DIR"/*.tar.gz | tail -n +8 | xargs rm -f
else
echo "Backup failed!" >&2
exit 1
fiAdd set -e at the top of scripts to exit on any error. Use set -x for debugging (prints each command before executing).
Boost Your Developer Workflow
Free online tools for encoding, formatting, hashing, and more.
Explore Dev ToolsFrequently Asked Questions
What is the difference between sudo and su?
sudo runs a single command as another user (usually root) and logs the action. su switches to another user’s shell session entirely. sudo is preferred for security as it provides an audit trail and requires only your password.
How do I find which process is using a specific port?
Use lsof -i :PORT (e.g., lsof -i :8080) or ss -tulpn | grep :PORT. Both show the process ID and name. You may need sudo to see all processes.
What does chmod 755 mean?
755 means owner can read, write, execute (7); group can read and execute (5); others can read and execute (5). In symbolic terms: rwxr-xr-x. Common for scripts and directories.
How do I run a command in the background and keep it running after logout?
Use nohup command & to run immune to hangups. For more control, use screen or tmux sessions, or run as a systemd service for production workloads.
How do I check system resource usage quickly?
Use top or htop for interactive monitoring. For quick checks: free -h for memory, df -h for disk space, uptime for load average, and ps aux --sort=-%cpu | head for top CPU consumers.