Operations 15 min read

Master Linux Command-Line: Advanced Ops Tricks & Error‑Fixing Guide

This comprehensive guide teaches Linux operations engineers advanced command‑line techniques, powerful one‑liners, and step‑by‑step troubleshooting for common errors, enabling faster issue resolution, higher efficiency, and proactive system maintenance.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Command-Line: Advanced Ops Tricks & Error‑Fixing Guide

Master Linux Command-Line: Advanced Ops Tricks & Error‑Fixing Guide

As a seasoned operations engineer, I have witnessed many incidents caused by insufficient command‑line knowledge. The following tips and error‑handling methods will dramatically boost your efficiency and help you resolve problems quickly.

Why mastering advanced command‑line matters

When an alert fires at 3 am, you need fast, reliable CLI skills rather than a GUI. Mastery can:

Increase efficiency by 300% : batch operations, pipelines, script automation

Reduce downtime by 70% : rapid diagnosis, precise targeting, instant fixes

Lower error rate by 80% : standardized, repeatable commands with history

Advanced techniques: boost your CLI operations

1. Advanced history usage

Most users only know the history command; experts use it like this:

# Search history for commands containing nginx
history | grep nginx

# Execute the most recent command that contains a keyword
!nginx

# Replace part of the previous command and run it
^old^new^

# Show timestamps for the last 10 commands
export HISTTIMEFORMAT="%F %T "
history 10

Practical scenario: Quickly re‑run a complex deployment command.

2. Ultimate text‑processing combos

# Count IP visits in a log and sort
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -20

# Real‑time error monitoring
tail -f /var/log/nginx/error.log | grep -i --color=always "error\|warning"

# Bulk replace configuration values
find /etc -name "*.conf" -exec sed -i 's/old_value/new_value/g' {} \;

# List largest directories
du -sh /* 2>/dev/null | sort -rh | head -20

3. Process management hacks

# Top CPU‑hungry processes
ps aux --sort=-%cpu | head -10

# Top memory‑hungry processes
ps aux --sort=-%mem | head -10

# Kill specific processes
pkill -f "nginx.*worker"

# Real‑time process monitoring
watch -n 1 'ps aux --sort=-%cpu | head -20'

4. Network troubleshooting toolkit

# Check port usage
ss -tuln | grep :80

# Summarize connection stats
ss -s

# Trace route
mtr google.com

# Live connection view
watch -n 1 'ss -tuln'

5. Disk space management mastery

# Find biggest directories (top 10)
du -sh /* 2>/dev/null | sort -rh | head -10

# Locate large files (>100M)
find / -size +100M -type f -exec ls -lh {} \; 2>/dev/null

# Clean package caches (Debian/Ubuntu)
sudo apt-get clean

# Clean caches (CentOS/RHEL)
sudo yum clean all

# Safely delete old temp files
sudo find /tmp -type f -atime +7 -delete

Common error troubleshooting guide

Error 1: Permission denied

Scenario:

$ ./deploy.sh
bash: ./deploy.sh: Permission denied

Steps:

# Check file permissions
ls -l deploy.sh

# Check file owner
stat deploy.sh

# Fix: add execute permission
chmod +x deploy.sh
# Or run with bash directly
bash deploy.sh

Error 2: No space left on device

Scenario:

$ cp large_file.zip /var/www/
cp: cannot create regular file '/var/www/large_file.zip': No space left on device

Steps:

# Check disk usage
df -h

# Check inode usage
df -i

# Find biggest directories
du -sh /var/* | sort -rh | head -5

# Locate large log files
find /var/log -name "*.log" -size +100M -mtime +30

# Clean logs and journal
sudo logrotate -f /etc/logrotate.conf
sudo journalctl --vacuum-time=30d

Error 3: Command not found

Scenario:

$ nginx -t
bash: nginx: command not found

Solution:

# Verify installation
which nginx
whereis nginx

# Check PATH
echo $PATH

# Locate binary
find /usr -name "nginx" 2>/dev/null

# Run with full path or add to PATH
/usr/sbin/nginx -t

Error 4: High system load

Scenario:

$ uptime
15:30:01 up 5 days, 2:15, 3 users, load average: 15.20, 12.50, 8.30

Investigation:

# Overall view
htop   # or top

# Top CPU consumers
ps aux --sort=-%cpu | head -20

# IO wait analysis
iostat -x 1

# Memory usage
free -h
cat /proc/meminfo

# Detailed per‑process stats
pidstat -u -r -d -h 1 10

Error 5: Port conflict

Scenario:

$ systemctl start nginx
Job for nginx.service failed. See 'journalctl -u nginx' for details.

Resolution:

# View service logs
journalctl -u nginx -f

# Check which process uses port 80
ss -tuln | grep :80
netstat -tulpn | grep :80

# Identify owning process
lsof -i :80

# Free the port (use with caution)
sudo fuser -k 80/tcp
# Or change nginx to listen on another port

Efficiency‑boosting tips

1. Useful aliases

# System monitoring
alias cpu='ps aux --sort=-%cpu | head -20'
alias mem='ps aux --sort=-%mem | head -20'
alias ports='ss -tuln'

# Log viewing
alias nginx-error='tail -f /var/log/nginx/error.log'
alias nginx-access='tail -f /var/log/nginx/access.log'
alias syslog='tail -f /var/log/syslog'

# Clean up
alias cleanup='sudo apt-get autoremove && sudo apt-get autoclean'

# Quick navigation
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

2. Wildcards and brace expansion

# Create project directories
mkdir -p project/{src,bin,doc,test}/{main,backup}

# Backup a config file
cp nginx.conf{,.bak}

# Batch chmod scripts
chmod +x script{1,2,3}.sh

# Create a series of files
touch file{01..10}.txt

3. Session management with screen/tmux

# Start a new session
screen -S deployment
tmux new -s deployment

# Detach (keep processes running)
Ctrl+A D   # screen
Ctrl+B D   # tmux

# Re‑attach
screen -r deployment
tmux attach -t deployment

Security best practices

1. Permission management

# Set appropriate file permissions
chmod 644 config.conf   # config file
chmod 755 script.sh     # executable script
chmod 600 private.key   # private key

# Elevate privileges safely
sudo -u www-data ls /var/www

2. Log auditing

# Recent login records
last -n 20
lastlog

# Failed authentication attempts
grep "Failed password" /var/log/auth.log

# Watch file changes
inotifywait -m /etc -e modify,create,delete

Performance tuning in practice

1. System performance monitoring

# General stats
vmstat 1 10
iostat -x 1 10
sar -u 1 10

# Detailed memory info
cat /proc/meminfo
slabtop

# Disk I/O analysis
iotop -o

2. Network performance optimization

# Connection count summary
ss -s

# Tune kernel parameters
sysctl net.core.somaxconn
sysctl net.ipv4.tcp_max_syn_backlog

# Real‑time traffic monitoring
iftop -i eth0

Emergency response handbook

System hang handling

# Check load
uptime

# Inspect memory
cat /proc/meminfo | grep -E "(MemTotal|MemFree|MemAvailable)"

# Check swap usage
swapon -s

# Force disk sync
sync

# If SSH works, restart the problematic service
systemctl restart high-cpu-service

Disk space emergency cleanup

# Quick cleanup script
#!/bin/bash
# Remove old large logs
sudo find /var/log -name "*.log" -mtime +7 -size +50M -delete

# Clean temporary files
sudo rm -rf /tmp/*

# Clean package manager cache
sudo apt-get clean

# Remove large core dumps
sudo find / -name "core.*" -size +10M -delete 2>/dev/null

Advanced automation techniques

1. Bulk server operations

# Run commands in parallel with pssh
pssh -h servers.txt -l root "systemctl status nginx"

# Loop over a range of servers
for server in web{1..5}; do
    ssh $server "uptime"
done

2. Smart monitoring scripts

#!/bin/bash
# System health check script
check_cpu() {
    load=$(uptime | awk '{print $10}' | sed 's/,//')
    if (( $(echo "$load > 10" | bc -l) )); then
        echo "WARNING: High CPU load: $load"
    fi
}

check_memory() {
    mem_usage=$(free | awk '/^Mem:/{printf "%.2f", $3/$2 * 100}')
    if (( $(echo "$mem_usage > 90" | bc -l) )); then
        echo "WARNING: High memory usage: ${mem_usage}%"
    fi
}

check_disk() {
    df -h | awk 'NR>1 {if($5+0 > 90) print "WARNING: Disk " $1 " usage: " $5}'
}

check_cpu
check_memory
check_disk

Conclusion: Keys to becoming a Linux ops expert

Rapid problem identification : use combined commands to gather system info quickly.

Efficient fault resolution : follow standardized procedures for common issues.

Preventive maintenance : monitor with scripts to spot potential problems early.

Automation : reduce repetitive tasks and boost operational efficiency.

Remember: A true ops expert doesn’t memorize every command; they know which tool fits each scenario and how to combine them to solve real problems.

图片
图片
LinuxTroubleshootingCommand Lineshell scripting
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.