AskDB
·8 min read

Linux Commands Cheat Sheet

Linux command line is essential for server management, DevOps, and development. This cheat sheet covers the most frequently used commands.

File Navigation

pwd                         # Current directory
ls -la                      # List all files with details
cd /path/to/dir             # Change directory
cd ~                        # Home directory
cd -                        # Previous directory

File Operations

cp file.txt backup.txt      # Copy
mv file.txt new.txt         # Move/rename
rm file.txt                 # Delete
rm -rf directory            # Delete directory recursively
mkdir -p dir/subdir         # Create directories
touch file.txt              # Create empty file
ln -s target link           # Create symlink

Viewing Files

cat file.txt                # Print file
less file.txt               # Scrollable view
head -n 20 file.txt         # First 20 lines
tail -n 20 file.txt         # Last 20 lines
tail -f log.txt             # Follow new lines
wc -l file.txt              # Count lines

Searching

find . -name "*.js"         # Find files by name
find . -type f -size +10M   # Files larger than 10MB
grep "pattern" file.txt     # Search in file
grep -r "pattern" .         # Recursive search
grep -i "pattern" file.txt  # Case insensitive
grep -n "pattern" file.txt  # Show line numbers

Permissions

chmod 755 script.sh         # rwxr-xr-x
chmod +x script.sh          # Add execute
chown user:group file.txt   # Change ownership
ls -l                       # View permissions

Process Management

ps aux                      # All processes
top                         # Live process monitor
kill PID                    # Kill process
kill -9 PID                 # Force kill
bg                          # Background last job
fg                          # Foreground last job
nohup command &             # Run after logout

Networking

curl https://example.com    # HTTP request
wget https://example.com    # Download file
ping google.com             # Test connectivity
netstat -tlnp               # Open ports
ss -tlnp                    # Open ports (modern)
ip addr                     # IP addresses

Text Processing

sort file.txt               # Sort lines
uniq file.txt               # Remove duplicates
cut -d',' -f1 file.csv      # Extract columns
awk '{print $1}' file.txt   # Print first column
sed 's/old/new/g' file.txt  # Replace text

Disk Usage

df -h                       # Disk space
du -sh *                    # Directory sizes
du -sh .                    # Current directory size

Useful Combos

# Count files in directory
ls | wc -l

# Find large files
find . -type f -size +100M -exec ls -lh {} ;

# Watch file for changes
watch -n 2 cat status.txt

# Search and replace in multiple files
find . -name "*.txt" -exec sed -i 's/old/new/g' {} ;

Tools

Use the Regex Tester to test grep patterns before running them on your files.