Whether you're deploying to a VPS, writing shell scripts, or debugging a production server at 2am, knowing Linux commands is non-negotiable for developers. This guide covers the commands you'll actually use, with real examples over theoretical explanations.
Navigation and File Management
# Navigation
pwd # print working directory
ls # list files
ls -la # list all (including hidden), long format
ls -lh # human-readable file sizes
cd /var/www/html # change directory (absolute path)
cd .. # go up one level
cd - # go to previous directory
cd ~ # go to home directory
# Creating and removing
mkdir my-project # create directory
mkdir -p path/to/nested # create nested directories
touch index.html # create empty file (or update timestamp)
cp file.txt backup.txt # copy file
cp -r src/ dest/ # copy directory recursively
mv old.txt new.txt # move/rename
rm file.txt # delete file
rm -rf dist/ # delete directory recursively (WARNING: no confirmation)
# Finding files
find . -name "*.php" # find by name pattern
find . -name "*.log" -mtime -7 # modified in last 7 days
find /var/log -size +100M # files larger than 100MB
locate config.php # fast search using indexed database (needs updatedb)
which node # find command binary location
type node # same, more detail
Reading and Editing Files
# Viewing files
cat file.txt # print entire file
less file.txt # paginated viewer (q to quit, /search to find)
head -n 20 file.txt # first 20 lines
tail -n 50 file.txt # last 50 lines
tail -f /var/log/nginx/error.log # follow file in real time (great for logs)
# Counting
wc -l file.txt # line count
wc -w file.txt # word count
wc -c file.txt # byte count
# Text editing (quick edits)
nano file.txt # beginner-friendly editor (Ctrl+O save, Ctrl+X exit)
vim file.txt # powerful but steep curve (i=insert, :wq=save+quit, :q!=quit without save)
# In-place text operations
sed -i 's/foo/bar/g' file.txt # replace all "foo" with "bar" in file
sed -i '/^#/d' .env.example # delete lines starting with #
sed -n '10,20p' file.txt # print lines 10 to 20
Searching Content: grep
grep "error" /var/log/nginx/error.log # search for "error" in file
grep -i "error" file.txt # case-insensitive
grep -r "TODO" src/ # recursive search in directory
grep -n "function" index.php # show line numbers
grep -l "API_KEY" .env* # list files containing pattern
grep -v "debug" app.log # invert match (lines NOT containing)
grep -c "error" app.log # count matching lines
grep -A 3 "FATAL" app.log # show 3 lines after each match
grep -B 2 -A 2 "Exception" app.log # 2 lines before and after
grep -E "(error|warning|fatal)" app.log # extended regex (multiple patterns)
# ripgrep (rg) — faster, smarter grep
rg "TODO" src/ # like grep -r but faster and respects .gitignore
rg -t php "function" # only PHP files
rg -l "console.log" . # just file names
Process Management
# Viewing processes
ps aux # all running processes
ps aux | grep node # find specific process
top # live process viewer
htop # improved top (may need: apt install htop)
# Process control
kill 12345 # send SIGTERM (graceful shutdown) to PID 12345
kill -9 12345 # send SIGKILL (force kill) — use only if SIGTERM fails
killall node # kill all processes named "node"
pkill -f "my-script.js" # kill by full command match
# Background processes
node server.js & # run in background, job number shown
jobs # list background jobs
fg %1 # bring job 1 to foreground
nohup node server.js & # run immune to hangup (continues after logout)
disown %1 # detach job from shell (won't die on logout)
# Resource usage
df -h # disk usage per filesystem
du -sh * # directory sizes in current folder
du -sh /var/log/* # sizes of all items in /var/log
free -h # memory usage
lscpu # CPU info
File Permissions
# Permission format: rwxrwxrwx (owner, group, others)
# r=read(4), w=write(2), x=execute(1)
chmod 755 script.sh # rwxr-xr-x — owner full, others read+execute
chmod 644 config.php # rw-r--r-- — owner read+write, others read
chmod 600 .env # rw------- — owner only (for secrets)
chmod +x deploy.sh # add execute for everyone
chmod -R 755 /var/www/html # recursive
chown www-data:www-data file.txt # change owner and group
chown -R www-data:www-data /var/www # recursive
# Check current permissions
ls -la file.txt # -rw-r--r-- 1 user group 1234 Jan 1 file.txt
stat file.txt # detailed file info including permission bits
# Real-world permissions for web apps
chmod 755 /var/www/html # web root accessible
chmod 644 /var/www/html/index.php # PHP files readable
chmod 600 /var/www/html/.env # secrets owner-only
chown -R www-data:www-data /var/www/html # Nginx/Apache owns the files
Networking
# HTTP requests
curl https://api.example.com/users # GET request
curl -X POST -d '{"key":"val"}' \
-H "Content-Type: application/json" \
https://api.example.com/create # POST with JSON body
curl -o output.html https://example.com # save response to file
curl -I https://example.com # headers only (HEAD request)
curl -u user:pass https://api.example.com # basic auth
wget https://example.com/file.tar.gz # download file
# Connectivity
ping google.com # test reachability
traceroute google.com # trace network path
nslookup example.com # DNS lookup
dig example.com A # detailed DNS query
dig example.com MX # mail exchange records
# Ports and connections
netstat -tulnp # listening ports and their PIDs
ss -tulnp # same, faster (modern replacement)
lsof -i :3000 # what's using port 3000
SSH
# Connecting
ssh user@192.168.1.100 # connect by IP
ssh user@example.com -p 2222 # custom port
# Key-based auth (passwordless SSH)
ssh-keygen -t ed25519 -C "your@email.com" # generate key pair
ssh-copy-id user@server # copy public key to server
cat ~/.ssh/id_ed25519.pub # view your public key
# SSH config file (~/.ssh/config)
# Host myserver
# HostName 192.168.1.100
# User deploy
# Port 2222
# IdentityFile ~/.ssh/my_key
ssh myserver # use shorthand after configuring
# File transfer
scp local.txt user@server:/remote/path/ # copy file to server
scp user@server:/remote/file.txt ./ # copy from server
rsync -avz --progress src/ user@server:/dest/ # sync directory (faster, resumable)
rsync -avz --delete src/ user@server:/dest/ # sync and remove extra files on dest
# Tunneling
ssh -L 5432:localhost:5432 user@server # forward local 5432 to server's PostgreSQL
ssh -L 8080:localhost:3000 user@server # access server's port 3000 at localhost:8080
Package Management
# Ubuntu/Debian (apt)
apt update # refresh package lists
apt upgrade # upgrade installed packages
apt install nginx # install package
apt remove nginx # remove package
apt search redis # search available packages
apt list --installed | grep php # list installed, filter
dpkg -l | grep nginx # alternative list check
# CentOS/RHEL/AlmaLinux (dnf / yum)
dnf update
dnf install nginx
dnf search nodejs
rpm -qa | grep php
# Universal (snap)
snap install node --channel=22/stable --classic
snap list
Useful Shell Tricks
# Pipes and redirection
command1 | command2 # pipe output of cmd1 to cmd2
echo "text" > file.txt # write (overwrite) to file
echo "text" >> file.txt # append to file
command 2> error.log # redirect stderr
command > output.log 2>&1 # redirect both stdout and stderr
# Command chaining
cmd1 && cmd2 # run cmd2 only if cmd1 succeeded
cmd1 || cmd2 # run cmd2 only if cmd1 failed
cmd1 ; cmd2 # run cmd2 regardless
# History and shortcuts
history # list command history
!! # repeat last command
!grep # repeat last command starting with "grep"
Ctrl+R # reverse search command history
Ctrl+C # cancel current command
Ctrl+Z # suspend to background
Ctrl+L # clear terminal
Ctrl+A / Ctrl+E # jump to start / end of line
# Variables and aliases
export MY_VAR="hello" # set environment variable
echo $MY_VAR # use it
alias ll='ls -la' # create alias (add to ~/.bashrc for persistence)
source ~/.bashrc # reload config without restarting shell
# Text processing with awk
awk '{print $1}' file.txt # print first column
awk -F: '{print $1}' /etc/passwd # use : as delimiter
df -h | awk '{print $5}' # disk usage percentages column
# Quick HTTP server for static files
python3 -m http.server 8080 # serves current directory at localhost:8080
php -S localhost:8080 # PHP built-in server
System Information
uname -a # kernel and OS info
cat /etc/os-release # distribution details
hostname # server hostname
uptime # uptime and load average
last # login history
whoami # current user
id # user and group IDs
env # all environment variables
printenv PATH # specific variable
date # current date/time
date +"%Y-%m-%d %H:%M:%S" # formatted date
Useful One-Liners
# Find the 10 largest files in /var
du -sh /var/* 2>/dev/null | sort -rh | head -10
# Show all PHP errors in the last hour
grep "$(date '+%Y/%m/%d %H')" /var/log/php/error.log | grep -i error | tail -50
# Count HTTP status codes in Nginx access log
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
# Watch disk usage every 2 seconds
watch -n 2 df -h
# Find and delete log files older than 30 days
find /var/log -name "*.log" -mtime +30 -exec rm {} \;
# Monitor a process by name
watch -n 1 'ps aux | grep node | grep -v grep'
# Port scanning (your own server)
nmap localhost
# Create a tar archive
tar -czf backup.tar.gz /var/www/html # compress
tar -xzf backup.tar.gz # extract
tar -tzf backup.tar.gz # list contents without extracting
Summary
Linux proficiency comes from daily use, not memorization. Bookmark this page, start using the commands that apply to your current work, and expand from there. The three highest-leverage areas to master first: grep for finding things, tail -f for debugging live logs, and chmod/chown for fixing permission errors that block deployments.
CREESOL