The Linux terminal is one of the most powerful tools a developer has. While it may seem intimidating at first, mastering essential commands makes you significantly more productive and gives you full control over your development environment and servers.
Navigation and file system exploration
# Show current directory
pwd
# List files and directories
ls -la # detailed listing with hidden files
ls -lah # human-readable sizes (KB, MB, GB)
ls -lt # sort by modification date
# Change directory
cd /var/log # absolute path
cd project/src # relative path
cd .. # go up one level
cd ~ # home directory
cd - # previous directory
# Create directories
mkdir -p project/src/components # create entire structure
# Directory tree (install: apt install tree)
tree -L 2 -I node_modules
File manipulation
# Create files
touch file.txt
echo "content" > file.txt # create with content (overwrites)
echo "more" >> file.txt # append
# Copy
cp file.txt copy.txt
cp -r folder/ backup/ # copy directory
# Move / rename
mv old.txt new.txt
mv file.txt target_dir/
# Delete
rm file.txt
rm -r folder/ # delete directory
rm -rf folder/ # force (careful!)
# View file contents
cat file.txt # show everything
less file.txt # paginated (q to quit)
head -20 file.txt # first 20 lines
tail -f /var/log/syslog # follow logs in real-time
# Find files
find . -name "*.js" # find .js files
find . -name "*.log" -mtime -7 # modified in last 7 days
find . -size +100M # files over 100MB
Searching file contents
# grep: search text in files
grep "error" app.log # search in file
grep -rn "TODO" ./src/ --include="*.js" # recursive with line numbers
grep -i "warning" *.log # case insensitive
grep -c "error" app.log # count occurrences
grep -v "debug" app.log # lines NOT containing "debug"
# Modern alternatives (faster)
# ripgrep: apt install ripgrep
rg "pattern" ./src/
rg -t js "import" # search only .js files
# Useful pipe combinations
cat access.log | grep "404" | wc -l # count 404 errors
ps aux | grep node # find Node.js processes
history | grep "docker" # search command history
Permissions and ownership
Every file has 3 permission levels: user (u), group (g), others (o), and 3 types: read (r=4), write (w=2), execute (x=1).
# Change permissions (octal)
chmod 755 script.sh # rwxr-xr-x
chmod 644 config.txt # rw-r--r--
chmod 600 .env # rw------- (owner only)
# Change permissions (symbolic)
chmod +x script.sh # add execute
chmod go-w file.txt # remove write for group/others
# Change ownership
chown user:group file.txt
chown -R www-data:www-data /var/www/ # recursive for web servers
# Common web project permissions
chmod -R 755 /var/www/html/
find /var/www/ -type f -exec chmod 644 {} \;
Processes and system resources
# View processes
ps aux # all processes
ps aux | grep nginx # find specific process
htop # interactive monitor (apt install htop)
# Kill processes
kill PID
kill -9 PID # force kill
killall node # kill all "node" processes
# Disk usage
df -h # partition space
du -sh * # size of each folder
du -h --max-depth=1 / | sort -rh | head # largest directories
# Memory
free -h
# System info
uname -a # kernel info
nproc # number of CPU cores
uptime # uptime and load average
Networking
# Network information
ip addr # interfaces and IPs
hostname -I # local IP
# Connectivity
ping -c 4 google.com
curl -I https://example.com # HTTP headers
curl -s https://api.example.com/data | jq . # API + format JSON
# Ports
ss -tlnp # listening TCP ports
lsof -i :3000 # what process uses port 3000
# Download files
wget https://example.com/file.zip
# Transfer files (SCP)
scp file.txt user@server:/path/
scp -r folder/ user@server:/path/
scp user@server:/path/file.txt .
SSH: remote server access
# Connect to a server
ssh user@server.com
# Generate SSH key
ssh-keygen -t ed25519 -C "your@email.com"
# Copy your public key to the server
ssh-copy-id user@server.com
# Run a remote command without interactive session
ssh user@server.com "df -h && free -h"
# SSH tunnel (access a remote service locally)
ssh -L 3307:localhost:3306 user@server.com
Compression
# tar + gzip
tar -czf backup.tar.gz my_folder/ # compress
tar -xzf backup.tar.gz # extract
tar -tzf backup.tar.gz # list contents
# zip
zip -r project.zip project/
unzip project.zip
Redirection and pipes
# Redirections
command > file.txt # save output (overwrites)
command >> file.txt # append
command 2> errors.txt # redirect errors only
command > all.txt 2>&1 # redirect both output and errors
# Useful pipes for developers
find . -name "*.js" -not -path "*/node_modules/*" | wc -l # count .js files
du -ah . | sort -rh | head -10 # 10 largest files
ps aux --sort=-%mem | head -10 # top memory consumers
tail -f /var/log/app.log | grep --line-buffered "ERROR" # monitor errors
Bash scripting basics
#!/bin/bash
# deploy.sh - Simple deployment script
set -e
APP_DIR="/var/www/my-app"
BACKUP_DIR="/var/backups/my-app"
DATE=$(date +%Y%m%d_%H%M%S)
echo "=== Starting deployment ==="
# Backup
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" -C $APP_DIR .
# Pull changes
cd $APP_DIR && git pull origin main
# Install dependencies
npm ci --production
# Restart
pm2 restart my-app
# Verify
sleep 3
if curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/api/health | grep -q "200"; then
echo "Deployment successful"
else
echo "ERROR: Rolling back..."
tar -xzf "$BACKUP_DIR/backup_$DATE.tar.gz" -C $APP_DIR
pm2 restart my-app
exit 1
fi
Quick reference
| Task | Command |
|---|---|
| Where am I | pwd |
| List files | ls -lah |
| Find file | find . -name "*.js" |
| Search text | grep -rn "text" ./src/ |
| Disk space | df -h |
| Folder size | du -sh folder/ |
| RAM usage | free -h |
| Processes | ps aux | grep name |
| Listening ports | ss -tlnp |
| Kill process | kill -9 PID |
| Permissions | chmod 755 file |
| Follow logs | tail -f file.log |
Resources
- LinuxCommand.org — Free interactive tutorial
- ExplainShell — Paste any command and get an explanation
- tldr pages — Simplified man pages (
npm install -g tldr) - OverTheWire Bandit — CTF game to practice Linux