dotlinux guide

Linux Command Line for Developers: Essential Techniques and Tools

For developers, the Linux command line is more than just a tool—it’s a gateway to efficiency, automation, and granular control over systems and workflows. Whether you’re managing remote servers, automating repetitive tasks, parsing logs, or debugging applications, proficiency with the command line (CLI) can drastically accelerate your productivity. Unlike graphical interfaces, the CLI offers scripting capabilities, remote access, and fine-grained control that are indispensable in modern development environments. This blog dives into essential CLI techniques and tools tailored for developers, from foundational navigation to advanced automation and system management. By the end, you’ll have the knowledge to streamline your workflow, troubleshoot issues faster, and leverage the full power of Linux in your development process.

Table of Contents

  1. Understanding the Linux Shell
  2. Core Navigation and File Operations
  3. Essential Command-Line Tools
  4. Text Processing Mastery
  5. Process Management
  6. Automation with Shell Scripting
  7. Advanced Tools for Developers
  8. Best Practices for Efficient CLI Usage
  9. Conclusion
  10. References

Understanding the Linux Shell

What is a Shell?

The shell is a command-line interpreter that acts as an interface between the user and the Linux kernel. It processes commands, executes programs, and manages input/output. The most common shell for developers is Bash (Bourne Again Shell), though alternatives like Zsh and Fish offer enhanced features (e.g., better autocompletion).

Accessing the Terminal

On Linux desktop environments (e.g., GNOME, KDE), launch the terminal via:

  • Keyboard shortcut: Ctrl+Alt+T (most distributions).
  • Application menu: Search for “Terminal” or “Console”.

On remote servers (e.g., AWS EC2, DigitalOcean), access the shell via SSH (covered later).

Core Navigation and File Operations

Master these to move efficiently through the filesystem:

CommandPurposeExample
pwdPrint working directory (current path)pwd/home/developer/projects
cd [dir]Change directorycd docs → Move to ./docs
cd ~Go to home directorycd ~/home/developer
cd ..Go up one directorycd .. → Move from /a/b/c to /a/b
ls [opts]List directory contentsls -la → List all files (including hidden) with details

Key ls options:

  • -l: Long format (permissions, owner, size, date).
  • -a: Show hidden files (names starting with .).
  • -h: Human-readable sizes (e.g., 1K, 2M).
  • -t: Sort by modification time (newest first).

File and Directory Management

Create, copy, move, and delete files/directories with these commands:

CommandPurposeExample
mkdir [dir]Create directorymkdir src tests → Create src/ and tests/
mkdir -p a/b/cCreate nested directories (no error if exists)mkdir -p logs/2024/jan
touch [file]Create empty file or update timestamptouch app.js → Create app.js
cp [src] [dest]Copy file/directorycp file.txt backup/ → Copy file.txt to backup/
cp -r [src] [dest]Recursively copy directoriescp -r src/ backup/src/
mv [src] [dest]Move/rename file/directorymv oldname.txt newname.txt
rm [file]Delete filerm temp.log
rm -r [dir]Recursively delete directoryrm -r old_dir/
ln -s [target] [link]Create symbolic link (like a shortcut)ln -s /usr/local/bin/node ~/bin/node

⚠️ Warning: rm -rf [path] force-deletes files/directories permanently (no trash bin). Avoid wildcards like rm -rf * in critical directories (e.g., /).

Essential Command-Line Tools

File Viewing and Inspection

Quickly inspect file contents without opening an editor:

CommandPurposeExample
cat [file]Print entire file to terminalcat README.md
less [file]Paginate through large files (navigate with j/k, exit with q)less large_log.txt
head -n [num] [file]Show first num lineshead -n 5 app.log → First 5 lines
tail -n [num] [file]Show last num linestail -n 10 error.log → Last 10 lines
tail -f [file]”Follow” a file (live updates, e.g., logs)tail -f /var/log/nginx/access.log

Searching and Filtering Text

Locate patterns in files or output with grep:

# Basic: Search for "error" in app.log (case-sensitive)
grep "error" app.log

# Case-insensitive search
grep -i "ERROR" app.log

# Recursive search in a directory (e.g., find "TODO" in src/)
grep -r "TODO" src/

# Show line numbers
grep -n "critical" server.log

# Invert match (show lines WITHOUT "debug")
grep -v "debug" app.log

Finding Files and Directories

Use find to locate files by name, type, size, or modification time:

# Find all .js files in current directory and subdirectories
find . -name "*.js"

# Find directories named "test"
find /home -type d -name "test"

# Find files larger than 100MB modified in the last 7 days
find /var/log -type f -size +100M -mtime -7

# Delete all .tmp files (use with caution!)
find . -name "*.tmp" -delete

Disk and System Monitoring

Check storage usage and system resources:

CommandPurposeExample
df -hDisk free space (human-readable)df -h/dev/sda1 20G 5G 15G 25% /
du -sh [dir]Disk usage of a directory (summary)du -sh node_modules/850M node_modules/
free -hMemory usagefree -hMem: 16G 5.2G 10G 320M 512M

Text Processing Mastery

Stream Editing with sed

sed (stream editor) modifies text in pipelines or files. Common use case: search-and-replace.

# Replace "old" with "new" in a file (print to terminal, no in-place change)
sed 's/old/new/' file.txt

# Replace all occurrences (global) in-place (backup original with .bak)
sed -i.bak 's/error/ERROR/g' app.log

# Delete lines containing "debug"
sed '/debug/d' app.log

# Insert "Header" at the top of the file
sed '1i Header' file.txt

Pattern Scanning with awk

awk is a powerful tool for processing structured text (e.g., CSVs, logs) by columns.

# Print the 3rd column of a CSV file (comma-separated)
awk -F ',' '{print $3}' data.csv

# Print lines where the 2nd column is "active"
awk '$2 == "active" {print $1, $3}' status.log

# Sum the 4th column (e.g., total sales)
awk '{sum += $4} END {print sum}' sales.csv

Combining Commands with Pipes

Use | (pipe) to chain commands, passing output of one as input to the next.

Example 1: Count 404 errors in an Nginx log
Nginx logs have columns like: IP - - [date] "GET /page" 404 123

# Step 1: Grep lines with "404", Step 2: Count them
cat /var/log/nginx/access.log | grep " 404 " | wc -l

Example 2: Find largest 5 files in a directory

du -h * | sort -rh | head -n 5
# `du -h *`: Size of all files/dirs, `sort -rh`: Sort human-readable descending, `head -n5`: Top 5

Process Management

Viewing Processes

Monitor running programs and their resource usage:

CommandPurposeExample
psList processes (current shell)ps → Shows processes in the current terminal
ps auxList all processes (system-wide)`ps aux
top/htopInteractive process monitor (real-time)htop → Better UI than top (install with sudo apt install htop)

Controlling Processes

Start, stop, and manage background/foreground tasks:

CommandPurposeExample
command &Run command in backgroundpython server.py & → Server runs in background
jobsList background jobsjobs[1]+ Running python server.py &
fg %1Bring job 1 to foregroundfg %1 → Resume server in terminal
bg %1Send stopped job 1 to backgroundbg %1 → Restart job 1 in background
kill [PID]Terminate process by ID (PID)kill 1234 → Stop process with PID 1234
kill -9 [PID]Force-kill unresponsive processkill -9 1234 → Use as last resort

Automation with Shell Scripting

Shell scripts (.sh) automate repetitive tasks. Here’s how to write them:

Script Basics: Shebang and Execution

Start scripts with a shebang to specify the shell, then make them executable with chmod +x.

#!/bin/bash
# This is a comment
echo "Hello, CLI!"

Run it:

chmod +x hello.sh  # Make executable
./hello.sh         # Execute (./ required for current directory)

Variables, Loops, and Conditionals

Variables: Store data with VAR=value (no spaces). Access with $VAR.

NAME="DevOps"
echo "Hello, $NAME!"  # Output: Hello, DevOps!

Loops: Iterate over files, lists, etc.

# Loop over .txt files and count lines
for file in *.txt; do
  echo "$file: $(wc -l < $file) lines"
done

Conditionals: Check file existence, exit codes, etc.

if [ -f "config.ini" ]; then
  echo "Config file found."
else
  echo "Error: config.ini missing!"
  exit 1  # Exit with error code
fi

Practical Script Example: Log Rotator

Automatically archive old logs and keep only the last 7 days:

#!/bin/bash
LOG_DIR="/var/log/myapp"
BACKUP_DIR="$LOG_DIR/backups"

# Create backup dir if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Archive logs older than 1 day
find "$LOG_DIR" -name "*.log" -type f -mtime +1 -exec mv {} "$BACKUP_DIR/" \;

# Compress backups older than 3 days
find "$BACKUP_DIR" -name "*.log" -type f -mtime +3 -exec gzip {} \;

# Delete backups older than 7 days
find "$BACKUP_DIR" -name "*.log.gz" -type f -mtime +7 -delete

echo "Log rotation complete."

Advanced Tools for Developers

Terminal Multiplexing with tmux

tmux lets you split the terminal into panes, create tabs (windows), and persist sessions (even after SSH disconnects).

Basic tmux commands:

  • tmux new -s [name]: Create a new session (e.g., tmux new -s dev).
  • tmux attach -t [name]: Reconnect to a session.
  • Ctrl+b %: Split pane vertically.
  • Ctrl+b ": Split pane horizontally.
  • Ctrl+b [arrow key]: Navigate panes.
  • Ctrl+b c: Create new window.
  • Ctrl+b d: Detach from session (session continues running).

Remote Access with SSH

Securely access remote servers via SSH (Secure Shell):

# Basic SSH login
ssh [email protected]

# SSH with a specific port (e.g., 2222)
ssh -p 2222 [email protected]

# Key-based authentication (more secure than passwords)
ssh-keygen -t ed25519  # Generate SSH key pair (store in ~/.ssh/)
ssh-copy-id [email protected]  # Copy public key to server (no more password prompts!)

Package Managers for Dev Tools

Install development tools (e.g., git, node, python) via package managers:

DistributionPackage ManagerInstall Example
Debian/Ubuntuaptsudo apt update && sudo apt install git
RHEL/CentOSyum/dnfsudo dnf install nodejs
Arch Linuxpacmansudo pacman -S python
macOS (Homebrew)brewbrew install go

Best Practices for Efficient CLI Usage

Productivity Hacks

  • Tab completion: Press Tab to auto-complete commands, file names, or arguments (e.g., cd proje[Tab]cd projects/).
  • Command history: Press Up/Down arrows to revisit recent commands. Use Ctrl+R to reverse-search history (type a keyword to find a past command).
  • Aliases: Shorten frequent commands. Add to ~/.bashrc or ~/.zshrc:
    alias ll='ls -la'
    alias gs='git status'
    alias dev='cd ~/developer/projects'
    Reload with source ~/.bashrc.

Safety and Security Tips

  • Avoid rm -rf with wildcards: Use rm -i (