Linux Command Line Essentials

Difficulty: Beginner

Question

What Linux commands should every developer know? Explain file navigation, text processing, and process management.

Answer

Linux command line proficiency is essential for DevOps and backend development. Servers run Linux, CI/CD pipelines run on Linux, and Docker containers are Linux-based.

File navigation and management: ls, cd, pwd, mkdir, cp, mv, rm, find, and ln for creating symbolic links.

Text processing: cat, head, tail, grep, sed, awk, sort, uniq, wc, and cut. These commands can be piped together using | to create powerful data processing pipelines.

Process management: ps, top/htop, kill, bg, fg, nohup, and systemctl for managing services.

Pipes and redirection: The | operator sends output of one command as input to another. > redirects output to a file (overwrite), >> appends. < reads input from a file. 2>&1 redirects stderr to stdout.

These commands form the foundation for shell scripting, debugging production issues, and working with Docker containers and CI/CD environments.

Code examples

File Navigation and Management

# Navigation
pwd                          # print working directory
ls -la                       # list all files with details
cd /var/log                  # change directory
cd -                         # go to previous directory
cd ~                         # go to home directory

# File operations
mkdir -p src/components      # create nested directories
cp -r src/ backup/           # copy directory recursively
mv old-name.ts new-name.ts   # rename/move file
rm -rf dist/                 # remove directory (CAREFUL!)
ln -s /path/to/file link     # create symbolic link

# Finding files
find . -name "*.ts" -type f         # find TypeScript files
find . -name "*.log" -mtime +7 -delete  # delete logs older than 7 days
find . -size +100M                  # find files larger than 100MB

# Disk usage
df -h                        # disk space (human readable)
du -sh */                    # size of each subdirectory
du -sh node_modules/         # size of node_modules

# File info
file image.png               # detect file type
stat package.json            # detailed file metadata
wc -l src//*.ts            # count lines in TypeScript files

find is incredibly versatile for locating files by name, size, date, or type. du -sh is essential for debugging disk space issues on servers.

Text Processing with Pipes

# View file contents
cat server.log                  # entire file
head -20 server.log             # first 20 lines
tail -50 server.log             # last 50 lines
tail -f server.log              # follow log in real-time

# Search with grep
grep "ERROR" server.log                 # find error lines
grep -r "TODO" src/                     # recursive search
grep -n "function" app.ts               # with line numbers
grep -c "404" access.log                # count matches
grep -v "DEBUG" server.log              # exclude DEBUG lines

# Pipe chains (powerful combinations)
cat access.log | grep "POST" | wc -l
# Count POST requests

cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
# Top 10 IP addresses by request count

ps aux | grep node | grep -v grep
# Find running Node.js processes

du -sh */ | sort -rh | head -5
# Top 5 largest directories

# Text transformation
sed 's/localhost/production.server.com/g' config.txt
# Replace all occurrences

awk -F',' '{print $1, $3}' data.csv
# Extract columns 1 and 3 from CSV

Pipes chain commands together: output of one becomes input of the next. grep + sort + uniq + awk can answer most log analysis questions.

Process Management

# View running processes
ps aux                      # all processes
ps aux | grep node          # find Node processes
top                         # interactive process viewer
htop                        # better interactive viewer

# Kill processes
kill 12345                  # graceful shutdown (SIGTERM)
kill -9 12345               # force kill (SIGKILL)
killall node                # kill all node processes
pkill -f "node server.js"   # kill by command pattern

# Background processes
node server.js &            # run in background
jobs                        # list background jobs
fg %1                       # bring job 1 to foreground
bg %1                       # resume job 1 in background
nohup node server.js &      # survives terminal close

# System services (systemd)
systemctl status nginx       # check service status
systemctl start nginx        # start a service
systemctl restart nginx      # restart a service
systemctl enable nginx       # start on boot
journalctl -u nginx -f       # follow service logs

# Port usage
lsof -i :3000               # what process uses port 3000
netstat -tlnp               # all listening ports
ss -tlnp                    # modern alternative to netstat

kill sends SIGTERM by default (graceful). Use kill -9 only as last resort. lsof -i :PORT is essential for debugging 'port already in use' errors.

Key points

Concepts covered

Linux Commands, File System, Processes, Pipes, Shell Scripting