Linux Commands Cheat Sheet

Everyday Linux: files, permissions, text tools, processes, networking, systemd and archives — one line each.

File Operations

ls -la  # List all files including hidden, with details
ls -lh  # List files with human-readable sizes
cd /path/to/dir  # Change to a directory
pwd  # Print the current working directory
cp file1 file2  # Copy a file
cp -r src/ dst/  # Copy a directory recursively
mv old new  # Rename a file or directory
mv file /tmp/  # Move a file to another directory
rm file  # Delete a file
rm -rf dir/  # Force-delete a directory and its contents (dangerous)
mkdir newdir  # Create a directory
mkdir -p a/b/c  # Create nested directories in one go
touch file  # Create an empty file or update its timestamp
ln -s target link  # Create a symbolic link
find . -name "*.log"  # Find files by name
find /var -mtime -7  # Files modified in the last 7 days
df -h  # Show disk space usage per filesystem

Permissions

chmod 755 script.sh  # rwxr-xr-x: owner can do all, others read+execute
chmod 644 file.txt  # rw-r--r--: owner read+write, others read-only
chmod 600 ~/.ssh/key  # rw-------: private files only the owner touches
chmod +x script.sh  # Make a file executable (for everyone)
chmod u+x script.sh  # Add execute permission for the owner only
chmod g-w file.txt  # Remove write permission from the group
chmod o=r file.txt  # Set others to read-only
chown user file  # Change the file owner
chown user:group file  # Change owner and group together
chown -R user dir/  # Change ownership recursively
chgrp group file  # Change the file's group

Text Processing

cat file  # Print the whole file to the terminal
less file  # Page through a file (q to quit)
head -n 20 file  # Show the first 20 lines
tail -n 20 file  # Show the last 20 lines
tail -f app.log  # Follow a log file live
wc -l file  # Count lines in a file
grep "error" file  # Find lines containing "error"
grep -r "TODO" .  # Search recursively in all files
grep -i "warn" file  # Case-insensitive search
grep -v "debug" file  # Show lines NOT matching
sort file  # Sort lines alphabetically
uniq file  # Remove adjacent duplicate lines
sort file | uniq -c  # Count occurrences of each unique line
awk '{print $2}' file  # Print the 2nd whitespace-separated column
sed 's/old/new/' file  # Replace first "old" with "new" per line
sed 's/old/new/g' file  # Replace all occurrences per line

Processes

ps aux  # List all running processes
ps aux | grep nginx  # Find a process by name
top  # Live process monitor (q to quit)
kill 1234  # Terminate process with PID 1234
kill -9 1234  # Force-kill a stuck process
killall nginx  # Kill all processes by name
nohup cmd &  # Run a command immune to hangup, in background

Network

ping -c 4 example.com  # Send 4 ICMP pings to a host
ss -tulpn  # List listening TCP/UDP ports with process names
ss -tlnp  # Listening TCP ports with owning processes
curl -I example.com  # Fetch only HTTP headers
curl -o file url  # Download a file to a name
wget url  # Download a file with wget
scp file user@host:/dst  # Copy a file to a remote host over SSH
ssh user@host  # Open an SSH session to a remote host
dig example.com  # DNS lookup

Systemd

sudo systemctl start nginx  # Start a service
sudo systemctl stop nginx  # Stop a service
sudo systemctl restart nginx  # Restart a service
sudo systemctl reload nginx  # Reload config without full restart
sudo systemctl status nginx  # Show service status
sudo systemctl enable nginx  # Start the service on boot
sudo systemctl disable nginx  # Don't start on boot
journalctl -u nginx  # Show logs for a service
journalctl -u nginx -f  # Follow a service's logs live
journalctl --since "1 hour ago"  # Logs from the last hour
journalctl -p err -b  # Errors from the current boot

Archives & Compression

tar -czvf arc.tar.gz dir/  # Create a gzip-compressed archive
tar -xzvf arc.tar.gz  # Extract a gzip-compressed archive
tar -tzvf arc.tar.gz  # List archive contents without extracting
tar -xjvf arc.tar.bz2  # Extract a bzip2-compressed archive
tar -xvf arc.tar -C /dst  # Extract a .tar to a specific directory
gzip file  # Compress a file (removes original)
gunzip file.gz  # Decompress a .gz file
zip -r arc.zip dir/  # Create a zip archive of a directory
unzip arc.zip  # Extract a zip archive