Core Idea: The Power of Command Combinations
The philosophy of Linux is that a program should do one thing and do it well. Combining multiple commands through pipes <span>|</span>, redirection <span>></span>, and logical operators <span>&&</span> and <span>||</span> is key to solving complex problems.
1. System Status and Monitoring
-
Real-time Monitoring of Core Metrics (Top Integrated Tool)
htop
- Scenario: Replaces
<span>top</span>, providing a more intuitive view of CPU, memory, and process load, supporting color, graphical display, and mouse operations.
Quickly View System Load and Uptime
uptime && free -h && df -h
- Scenario: Get a one-click overview of the system’s overall health.
<span>uptime</span>: View uptime, number of users, and average load over 1/5/15 minutes.<span>free -h</span>: View memory usage in a human-readable format (G/M).<span>df -h</span>: View disk space usage in a human-readable format.
Monitor Disk I/O Activity
iostat -dx 2
- Scenario: Determine if the disk is a performance bottleneck.
<span>-d</span>shows device reports,<span>-x</span>shows extended statistics, and<span>2</span>indicates refresh every 2 seconds. Focus on<span>%util</span>(utilization, close to 100% indicates saturation) and<span>await</span>(average wait time for I/O).
Monitor Network Traffic and Connections
iftop -P
- Scenario: View in real-time which network connections are consuming a lot of bandwidth, similar to
<span>top</span>for processes.<span>-P</span>shows port numbers.
Find the Most CPU-Intensive Processes
ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu | head -n 10
- Scenario: Quickly locate the top 10 processes consuming the most CPU.
<span>--sort=-%cpu</span>sorts by CPU usage in descending order.
Find the Most Memory-Intensive Processes
ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%mem | head -n 10
- Scenario: Quickly locate the top 10 processes consuming the most memory.
2. Log Analysis “Killer Tricks”
-
Real-time Tracking of Log Files (Most Common)
tail -f /var/log/nginx/access.log
- Scenario: View the latest log content in real-time, essential for debugging web requests.
<span>-f</span>(follow) parameter is key.
Filter Log Lines with the Keyword “error”
grep -i "error" /var/log/syslog
- Scenario: Quickly locate errors in a sea of logs.
<span>-i</span>ignores case.
Dynamic Log Tracking (Advanced Version of tail -f + grep)
tail -f /var/log/your_app.log | grep --line-buffered "keyword"
- Scenario: Only display logs in real-time that match specific keywords you care about.
<span>--line-buffered</span>ensures immediate output after each line matches.
View the Last 100 Lines of Logs and Filter by Keyword
tail -n 100 /var/log/nginx/access.log | grep "POST"
Count Access Times of Different IPs in Logs
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -n 10
- Scenario: Quickly find the top 10 most frequently accessed IPs for security or traffic analysis.
<span>awk '{print $1}'</span>: The first column of the default nginx log is the IP, extract it.<span>sort</span>: Sort to prepare for deduplication.<span>uniq -c</span>: Count occurrences of duplicate lines.<span>sort -nr</span>: Sort in reverse numerical order (from largest to smallest).<span>head -n 10</span>: Display the top 10 lines.
Analyze Logs for “504 Gateway Time-out” Errors
grep "504" /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -nr
- Scenario: Identify which request URLs most frequently cause 504 timeout errors.
3. File and Directory Operations
-
Recursively Find Files Containing Specific Content
grep -r "config_value" /etc/
- Scenario: Forgot which file a configuration item is in? This is the solution.
<span>-r</span>indicates recursive search.
Find Files by Filename
find /home -name "*.log"
- Scenario: Search for all files ending with
<span>.log</span>in the<span>/home</span>directory.
Find and Delete Log Files Older than 7 Days
find /var/log -name "*.log" -mtime +7 -exec rm -f {} \;
- Scenario: Log cleanup, regular archiving. Very dangerous, confirm the path and conditions before executing!
<span>-mtime +7</span>: Modified time older than 7 days.<span>-exec ... \;</span>: Execute the following command on each found file.
Quickly Calculate Directory Size
du -sh /var/log/
- Scenario: View the total size of a directory.
<span>-s</span>shows the total,<span>-h</span>displays in a human-readable format (G/M/K).
Find the 10 Largest Files in the System
find / -type f -exec du -Sh {} + 2>/dev/null | sort -rh | head -n 10
- Scenario: When disk space is low, quickly locate the “culprits”.
<span>2>/dev/null</span>: Ignore permission denied and other error messages.<span>sort -rh</span>: Sort in reverse order by human-readable numbers.
Compare Differences Between Two Directories
diff -rq dir1/ dir2/
- Scenario: After deploying code, confirm whether files are synchronized successfully.
<span>-r</span>compares recursively,<span>-q</span>reports only which files differ.
4. Network Troubleshooting and Debugging
-
Check Port Connectivity (Most Common)
nc -zv example.com 443
- Scenario: Quickly determine if a remote server’s port is open and reachable. More concise than telnet.
View Route Traces
traceroute -w 2 google.com
- Scenario: When the network is down, determine where the fault occurs at which hop.
<span>-w 2</span>sets the wait time to 2 seconds to avoid long waits.
View All Listening Ports and Corresponding Processes
ss -tulnp
- Scenario: Replaces
<span>netstat</span>, faster and more efficient. View which services are open on the local machine. <span>-t</span>: TCP,<span>-u</span>: UDP,<span>-l</span>: Listening,<span>-n</span>: Do not resolve service names,<span>-p</span>: Show process information.
View All Current Network Connections
ss -tan | awk '{print $NF}' | grep -v 'Address\|^$' | sort | uniq -c | sort -nr
- Scenario: Count connections in various states to determine if there are issues like SYN Flood or excessive TIME_WAIT.
Download Files (Supports Resuming)
wget -c http://example.com/bigfile.iso
- Scenario: Download large files.
<span>-c</span>parameter indicates resuming downloads.
5. Process Management
-
Gracefully Terminate a Process
kill -TERM
- Scenario: First send a
<span>TERM</span>signal, allowing the process to clean up before exiting, which is a more friendly way to end.
Forcefully Kill a Process
kill -9
- Scenario: When
<span>kill -TERM</span>is ineffective, use the ultimate measure<span>-9</span>(SIGKILL). Last resort.
Kill a Process by Name
pkill -f "python my_script.py"
- Scenario: No need to check PID first, directly kill the process by matching its name.
<span>-f</span>matches the entire command line.
Run a Process Reliably in the Background
nohup ./long_running_script.sh > script.log 2>&1 &
- Scenario: The process will not be terminated even after closing the terminal.
<span>nohup</span>: Ignores hangup signals.<span>> script.log</span>: Redirect standard output to a file.<span>2>&1</span>: Redirect standard error to standard output (i.e., the same file).<span>&</span>: Run in the background.
6. Data Processing and Backup
-
One-click Package and Compress Directory
tar -czvf backup.tar.gz /path/to/directory/
- Scenario: Archive and compress logs, code, data, etc.
<span>-c</span>creates,<span>-z</span>compresses with gzip,<span>-v</span>shows progress,<span>-f</span>specifies filename.
Extract Compressed Package
tar -xzvf backup.tar.gz
- Scenario: Extract common
<span>.tar.gz</span>files.<span>-x</span>indicates extraction.
View Command Output in Real-time and Write to File Simultaneously
./my_script.sh | tee output.log
- Scenario: Want to see real-time output on the screen while saving a log for later analysis.
<span>tee</span>command solves this perfectly.
Summary and Reminders
- Use
<span>rm -rf</span>with caution: Always double-check the path you want to delete. You can set<span>alias rm='rm -i'</span>to prompt before each deletion. - Make Good Use of
<span>man</span>:<span>man <command></span>is your best teacher, showing all parameters and detailed descriptions of commands. - Understand the Principles: Memorizing is not as good as understanding the meaning of each parameter and combination, so you can use them flexibly.
By mastering these 30 combinations, you can indeed solve most daily operational issues, greatly enhancing your work efficiency.