Boost Your Linux Ops: Advanced CLI Tricks and Fast Error Fixes
This guide equips Linux operations engineers with powerful command‑line shortcuts, automation tips, and step‑by‑step troubleshooting procedures for common errors such as permission issues, disk‑space exhaustion, missing commands, high load, and port conflicts, dramatically improving incident response speed and system reliability.
Why Master Advanced CLI Techniques?
When a 3 am alert fires, a seasoned operator relies on concise, composable shell commands rather than graphical tools. Mastery of the command line can triple efficiency, cut mean‑time‑to‑repair by 70 %, and reduce human error through repeatable, auditable actions.
Advanced Command‑Line Tricks
1. Powerful History Searches
# Find all previous nginx commands
history | grep nginx
# Re‑execute the last command containing "nginx"
!nginx
# Substitute a part of the previous command
^old^new^
# Show timestamps for the last 10 commands
export HISTTIMEFORMAT="%F %T "
history 102. Text‑Processing One‑Liners
# Top 20 IPs by request count
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"
# Batch replace in configuration files
find /etc -name "*.conf" -exec sed -i 's/old_value/new_value/g' {} \;
# List largest directories
du -sh /* 2>/dev/null | sort -rh | head -103. Process Management Hacks
# Highest CPU consumers
ps aux --sort=-%cpu | head -10
# Highest memory consumers
ps aux --sort=-%mem | head -10
# Kill all nginx workers
pkill -f "nginx.*worker"
# Live resource view
watch -n 1 'ps aux --sort=-%cpu | head -20'4. Network Diagnosis Toolkit
# Check if port 80 is in use
ss -tuln | grep :80
# Summarize socket usage
ss -s
# Trace route to a host
mtr google.com
# Continuously watch socket list
watch -n 1 'ss -tuln'5. Disk‑Space Management
# Largest directories
du -sh /* 2>/dev/null | sort -rh | head -10
# Find files >100 M
find / -size +100M -type f -exec ls -lh {} \;
# Clean package caches (Debian/Ubuntu)
sudo apt-get clean
# Clean package caches (CentOS/RHEL)
sudo yum clean all
# Remove old temporary files
sudo find /tmp -type f -atime +7 -deleteCommon Error‑Handling Playbooks
Permission Denied
# Reproduce
$ ./deploy.sh
bash: ./deploy.sh: Permission denied
# Diagnose
ls -l deploy.sh
stat deploy.sh
# Fix
chmod +x deploy.sh # add execute bit
# or run via explicit interpreter
bash deploy.shNo Space Left on Device
# Diagnose
df -h # overall usage
df -i # inode usage
du -sh /var/* | sort -rh | head -5 # biggest dirs
find /var/log -name "*.log" -size +100M -mtime +30 # large logs
# Clean
sudo logrotate -f /etc/logrotate.conf
sudo journalctl --vacuum-time=30dCommand Not Found
# Check installation
which nginx
whereis nginx
# Verify PATH
echo $PATH
# Locate binary manually
find /usr -name "nginx" 2>/dev/null
# Run with full path or adjust PATH
/usr/sbin/nginx -tHigh System Load
# Overview
htop # or top
# Top CPU hogs
ps aux --sort=-%cpu | head -20
# IO wait
iostat -x 1
# Memory snapshot
free -h
cat /proc/meminfo
# Detailed per‑process stats
pidstat -u -r -d -h 1 10Port Conflict
# View service logs
journalctl -u nginx -f
# Find occupying process
ss -tuln | grep :80
netstat -tulpn | grep :80
lsof -i :80
# Resolve
sudo fuser -k 80/tcp # force kill (use with care)
# or change nginx listen portProductivity Boosters
Useful Aliases
# Add to ~/.bashrc or ~/.zshrc
alias cpu='ps aux --sort=-%cpu | head -20'
alias mem='ps aux --sort=-%mem | head -20'
alias ports='ss -tuln'
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'
alias cleanup='sudo apt-get autoremove && sudo apt-get autoclean'
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'Brace Expansion & Globbing
# Create project skeleton
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
# Generate numbered files
touch file{01..10}.txtScreen/Tmux Session Management
# Start a new session
screen -S deployment
# or
tmux new -s deployment
# Detach (keep running)
Ctrl+A D # screen
Ctrl+B D # tmux
# Re‑attach
screen -r deployment
# or
tmux attach -t deploymentSecurity Best Practices
Permission Hardening
# Files
chmod 644 config.conf
chmod 755 script.sh
chmod 600 private.key
# Run as specific user
sudo -u www-data ls /var/wwwLog Auditing
# Recent logins
last -n 20
lastlog
# Failed SSH attempts
grep "Failed password" /var/log/auth.log
# Watch file changes
inotifywait -m /etc -e modify,create,deletePerformance Tuning
System Monitoring
vmstat 1 10
iostat -x 1 10
sar -u 1 10
cat /proc/meminfo
slabtop
iotop -oNetwork Optimization
# Socket stats
ss -s
# Kernel network parameters
sysctl net.core.somaxconn
sysctl net.ipv4.tcp_max_syn_backlog
# Live traffic view
iftop -i eth0Emergency Response Playbook
System Hang Recovery
# Check load
uptime
# Inspect memory
cat /proc/meminfo | grep -E "(MemTotal|MemFree|MemAvailable)"
# Verify swap
swapon -s
# Sync disks
sync
# If SSH works, restart offending service
systemctl restart high-cpu-serviceDisk‑Space Emergency Cleanup
#!/bin/bash
# Remove old large logs
sudo find /var/log -name "*.log" -mtime +7 -size +50M -delete
# Clean /tmp
sudo rm -rf /tmp/*
# Package cache cleanup
sudo apt-get clean
# Delete core dumps
sudo find / -name "core.*" -size +10M -exec rm -f {} \; 2>/dev/nullAdvanced Automation
Parallel Server Operations
# Run a command on many hosts
pssh -h servers.txt -l root "systemctl status nginx"
# Simple loop over a range
for server in web{1..5}; do
ssh $server "uptime"
doneSmart Monitoring Script
#!/bin/bash
check_cpu() {
load=$(uptime | awk '{print $10}' | sed 's/,//')
(( $(echo "$load > 10" | bc -l) )) && echo "WARNING: High CPU load: $load"
}
check_memory() {
mem=$(free | awk '/^Mem:/{printf "%.2f", $3/$2*100}')
(( $(echo "$mem > 90" | bc -l) )) && echo "WARNING: High memory usage: $mem%"
}
check_disk() {
df -h | awk 'NR>1 && $5+0>90 {print "WARNING: Disk " $1 " usage: " $5}'
}
check_cpu
check_memory
check_diskConclusion
By applying these advanced shell techniques, standardized error‑handling workflows, and lightweight automation scripts, Linux operators can locate problems instantly, resolve incidents with minimal downtime, and proactively prevent future failures, turning routine maintenance into a high‑efficiency, repeatable process.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
