Back to all posts

Linux Command Line Mastery: Essential Commands Every Developer Should Know

Master the Linux command line with this comprehensive guide covering essential commands, shortcuts, and advanced techniques for developers and system administrators.

Linux Command Line Mastery: Essential Commands Every Developer Should Know

Linux Command Line Mastery: Essential Commands Every Developer Should Know

The command line is the heart of Linux and a powerful tool for developers. Mastering these essential commands will dramatically improve your productivity and system administration skills.

File and Directory Operations

Basic Navigation

# Show current directory
pwd

# List directory contents
ls                    # Basic listing
ls -la               # Detailed with hidden files
ls -lh               # Human-readable file sizes
ls -lt               # Sort by modification time
ls -lS               # Sort by file size

# Change directory
cd /path/to/directory
cd ~                 # Home directory
cd -                 # Previous directory
cd ..                # Parent directory
cd ../..             # Two levels up

File Management

# Create files and directories
touch file.txt                    # Create empty file
mkdir directory                   # Create directory
mkdir -p path/to/nested/dir      # Create nested directories

# Copy files and directories
cp file.txt backup.txt           # Copy file
cp -r source/ destination/       # Copy directory recursively
cp -a source/ destination/       # Archive mode (preserves attributes)

# Move and rename
mv oldname.txt newname.txt       # Rename file
mv file.txt /path/to/destination/ # Move file
mv *.txt backup/                 # Move all .txt files

# Remove files and directories
rm file.txt                      # Remove file
rm -r directory/                 # Remove directory recursively
rm -f file.txt                   # Force remove (no prompt)
rm -rf directory/                # Force remove directory

File Permissions

# Change permissions
chmod 755 script.sh              # rwxr-xr-x
chmod +x script.sh               # Add execute permission
chmod -w file.txt                # Remove write permission
chmod u+x,g-w,o-r file.txt      # Complex permissions

# Change ownership
chown user:group file.txt        # Change user and group
chown -R user:group directory/   # Recursive ownership change
sudo chown root:root /etc/config # Change to root ownership

Text Processing and Searching

Viewing File Contents

# Display file contents
cat file.txt                     # Show entire file
head file.txt                    # First 10 lines
head -n 20 file.txt              # First 20 lines
tail file.txt                    # Last 10 lines
tail -n 20 file.txt              # Last 20 lines
tail -f /var/log/syslog          # Follow file changes (live)

# Page through large files
less file.txt                    # Scrollable viewer
more file.txt                    # Simple pager

Text Search and Manipulation

# Search within files
grep "pattern" file.txt          # Search for pattern
grep -i "pattern" file.txt       # Case-insensitive search
grep -r "pattern" directory/     # Recursive search
grep -n "pattern" file.txt       # Show line numbers
grep -v "pattern" file.txt       # Invert match (exclude pattern)
grep -E "pattern1|pattern2" file.txt  # Extended regex (OR)

# Advanced grep examples
grep -l "TODO" *.js              # Show only filenames with matches
grep -c "error" logfile.txt      # Count matches
grep -A 3 -B 2 "pattern" file.txt # Show 3 lines after, 2 before

Text Processing Tools

# Sort and unique
sort file.txt                    # Sort lines alphabetically
sort -n numbers.txt              # Numeric sort
sort -r file.txt                 # Reverse sort
uniq file.txt                    # Remove duplicate lines
sort file.txt | uniq -c          # Count occurrences

# Field processing
cut -d',' -f1,3 data.csv        # Extract columns 1 and 3 from CSV
cut -c1-10 file.txt             # Extract characters 1-10
awk '{print $1}' file.txt       # Print first field
awk -F',' '{print $2}' data.csv # Use comma as field separator

# Text replacement
sed 's/old/new/g' file.txt      # Replace all occurrences
sed -i 's/old/new/g' file.txt   # Edit file in-place
sed '1,5s/old/new/g' file.txt   # Replace only in lines 1-5

System Information and Monitoring

System Overview

# System information
uname -a                         # System information
whoami                          # Current user
id                              # User and group IDs
uptime                          # System uptime and load
date                            # Current date and time
cal                             # Calendar

# Hardware information
lscpu                           # CPU information
lsmem                           # Memory information
lsblk                           # Block devices
lsusb                           # USB devices
lspci                           # PCI devices

Process Management

# View processes
ps aux                          # All running processes
ps aux | grep process_name      # Find specific process
top                             # Real-time process viewer
htop                            # Enhanced top (if installed)
pstree                          # Process tree

# Process control
kill PID                        # Terminate process by PID
kill -9 PID                     # Force kill process
killall process_name            # Kill all processes by name
pkill process_name              # Kill processes by name pattern
nohup command &                 # Run command in background

System Resources

# Memory usage
free -h                         # Memory usage (human-readable)
free -m                         # Memory usage in MB

# Disk usage
df -h                           # Disk space usage
du -h directory/                # Directory size
du -sh *                        # Size of all items in current dir
ncdu                            # Interactive disk usage analyzer

# Network information
ip addr show                    # Network interfaces
ip route show                   # Routing table
netstat -tuln                   # Network connections
ss -tuln                        # Modern netstat alternative

Network and Connectivity

Network Tools

# Test connectivity
ping google.com                 # Test internet connectivity
ping -c 4 192.168.1.1          # Ping 4 times
traceroute google.com           # Trace network route
mtr google.com                  # Continuous traceroute

# Download files
wget https://example.com/file.zip
curl -O https://example.com/file.zip
curl -L -o output.html https://example.com  # Follow redirects

# Transfer files
scp file.txt user@host:/path/   # Secure copy to remote host
rsync -av source/ destination/  # Sync directories
rsync -av --delete src/ dest/   # Sync and delete extra files

SSH and Remote Access

# SSH connections
ssh user@hostname               # Connect to remote host
ssh -p 2222 user@hostname      # Connect on specific port
ssh -i ~/.ssh/key user@hostname # Use specific key

# SSH tunneling
ssh -L 8080:localhost:80 user@host  # Local port forwarding
ssh -R 8080:localhost:80 user@host  # Remote port forwarding
ssh -D 1080 user@host              # SOCKS proxy

Advanced Command Line Techniques

Pipes and Redirection

# Pipes
command1 | command2             # Pipe output to next command
ps aux | grep nginx | head -5   # Chain multiple commands

# Redirection
command > file.txt              # Redirect output to file
command >> file.txt             # Append output to file
command 2> errors.txt           # Redirect errors to file
command > output.txt 2>&1       # Redirect both output and errors
command < input.txt             # Use file as input

Command Substitution

# Command substitution
echo "Today is $(date)"
echo "Files: $(ls | wc -l)"
backup_$(date +%Y%m%d).tar.gz   # Use command output in filename

# Variable assignment
current_dir=$(pwd)
file_count=$(ls | wc -l)
echo "Current directory: $current_dir has $file_count files"

History and Shortcuts

# Command history
history                         # Show command history
!123                           # Execute command number 123
!!                             # Execute last command
!ssh                           # Execute last command starting with 'ssh'
ctrl+r                         # Search command history interactively

# Keyboard shortcuts
ctrl+a                         # Move to beginning of line
ctrl+e                         # Move to end of line
ctrl+k                         # Delete from cursor to end
ctrl+u                         # Delete from cursor to beginning
ctrl+w                         # Delete word before cursor
ctrl+l                         # Clear screen

File Compression and Archives

Tar Archives

# Create archives
tar -czf archive.tar.gz directory/     # Create compressed tar
tar -cjf archive.tar.bz2 directory/    # Create bzip2 compressed tar
tar -cf archive.tar files/             # Create uncompressed tar

# Extract archives
tar -xzf archive.tar.gz                # Extract gzipped tar
tar -xjf archive.tar.bz2               # Extract bzip2 tar
tar -xf archive.tar                    # Extract uncompressed tar
tar -xzf archive.tar.gz -C /path/      # Extract to specific directory

# List archive contents
tar -tzf archive.tar.gz                # List files in gzipped tar
tar -tf archive.tar                    # List files in tar

Other Compression Tools

# Zip files
zip -r archive.zip directory/          # Create zip archive
unzip archive.zip                      # Extract zip archive
unzip -l archive.zip                   # List zip contents

# Individual file compression
gzip file.txt                          # Compress file (creates file.txt.gz)
gunzip file.txt.gz                     # Decompress file
bzip2 file.txt                         # Better compression
bunzip2 file.txt.bz2                   # Decompress bzip2

System Administration

Service Management (systemd)

# Service control
sudo systemctl start service_name      # Start service
sudo systemctl stop service_name       # Stop service
sudo systemctl restart service_name    # Restart service
sudo systemctl enable service_name     # Enable at boot
sudo systemctl disable service_name    # Disable at boot
sudo systemctl status service_name     # Check service status

# System control
sudo systemctl reboot                  # Restart system
sudo systemctl poweroff               # Shutdown system

Package Management

# Debian/Ubuntu (apt)
sudo apt update                        # Update package list
sudo apt upgrade                       # Upgrade packages
sudo apt install package_name          # Install package
sudo apt remove package_name           # Remove package
sudo apt search keyword                # Search packages
apt list --installed                   # List installed packages

# Red Hat/CentOS (yum/dnf)
sudo yum update                        # Update packages
sudo yum install package_name          # Install package
sudo dnf install package_name          # Install package (newer systems)

Log Files

# System logs
journalctl                             # View systemd logs
journalctl -u service_name             # Logs for specific service
journalctl -f                          # Follow log output
journalctl --since "2024-01-01"       # Logs since date

# Traditional log files
tail -f /var/log/syslog               # Follow system log
grep "error" /var/log/apache2/error.log # Search web server errors

Productivity Tips

Aliases and Functions

# Create aliases in ~/.bashrc
alias ll='ls -la'
alias grep='grep --color=auto'
alias ..='cd ..'
alias ...='cd ../..'

# Useful aliases
alias update='sudo apt update && sudo apt upgrade'
alias ports='netstat -tuln'
alias myip='curl ipinfo.io/ip'

# Functions
function mkcd() {
    mkdir -p "$1" && cd "$1"
}

Environment Variables

# Set environment variables
export PATH=$PATH:/new/path
export EDITOR=nano
export JAVA_HOME=/usr/lib/jvm/java-11

# Add to ~/.bashrc for persistence
echo 'export PATH=$PATH:/new/path' >> ~/.bashrc
source ~/.bashrc                      # Reload configuration

Screen/Tmux for Session Management

# Screen
screen                                # Start new session
screen -S session_name               # Named session
screen -r session_name               # Reattach to session
ctrl+a d                             # Detach from session

# Tmux (more modern alternative)
tmux                                 # Start new session
tmux new -s session_name             # Named session
tmux attach -t session_name          # Attach to session
tmux list-sessions                   # List sessions

Troubleshooting Commands

System Diagnostics

# Check system health
dmesg | tail                         # Kernel messages
lsof -i :80                         # What's using port 80
lsof /path/to/file                  # What's using a file
fuser -v /path/to/file              # Alternative to lsof

# Network troubleshooting
ping -c 4 8.8.8.8                  # Test internet connectivity
nslookup google.com                 # DNS lookup
dig google.com                      # DNS information

Performance Analysis

# I/O monitoring
iostat                              # I/O statistics
iotop                               # I/O usage by process
vmstat                              # Virtual memory statistics

# File system check
fsck /dev/sda1                      # Check filesystem
mount | grep sda1                   # Check mount options

Mastering these commands will make you significantly more productive when working with Linux systems. Practice them regularly, create useful aliases, and gradually incorporate more advanced techniques into your daily workflow.

Remember: The command line is incredibly powerful, but with great power comes great responsibility. Always double-check destructive commands, especially those involving rm, chmod, and system configuration changes.