Essential Linux Shell Commands for System Monitoring and Troubleshooting
This guide compiles a comprehensive set of Linux shell commands for deleting zero‑byte files, inspecting processes, checking CPU and memory usage, monitoring network connections, managing services, and includes a collection of practical regular expressions for common text‑processing tasks.
Key System Monitoring Commands
The following commands help administrators quickly assess the health and performance of a Linux server. find -type f -size 0 -exec rm -rf {} \; Delete all zero‑byte files. PS -e -o "%C : %p : %z : %a" | sort -k5 -nr List processes sorted by memory usage (largest first). ps -e -o "%C : %p : %z : %a" | sort -nr List processes sorted by CPU utilization.
grep -r -a jpg /data/cache/* | strings | grep "http:" | awk -F'http:' '{print "http:"$2;}'Print URLs of cached JPEG files.
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'Show the number of HTTP connections and their TCP states. sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config Replace no with yes in the Root line of sshd_config.
ps aux | grep mysql | grep -v grep | awk '{print $2}' | xargs kill -9Kill all MySQL processes. killall -TERM mysqld Gracefully terminate the MySQL daemon. ls /etc/rc3.d/S* | cut -c 15- List services started at run‑level 3 (demonstrates cut usage).
cat << EOF
+--------------------------------------------------------------+
| === Welcome to Tunoff services === |
+--------------------------------------------------------------+
EOFShow how to output multiple lines using a here‑document.
cd /usr/local/mysql/bin
for i in *
do ln /usr/local/mysql/bin/$i /usr/bin/$i
doneCreate symbolic links for all MySQL binaries in /usr/bin.
ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 6-Extract the IP address of eth0. free -m | grep "Mem" | awk '{print $2}' Show total memory size in megabytes. cat /proc/loadavg Display the three‑minute load average values. mpstat 1 1 Show CPU usage statistics; check that %idle is not too low. df -h Report disk space usage in human‑readable format. du -cks * | sort -rn | head -n 10 Find the ten largest files or directories in the current partition. iostat -x 1 2 Show detailed I/O statistics; verify that %util does not exceed 100%. sar -n DEV Display network traffic statistics (rx/tx bytes per second). netstat -i Check for network errors such as drops, collisions, or carrier problems.
lsof -i :80 | grep -v "ID" | awk '{print "kill -9",$2}' | shKill processes listening on port 80.
ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9Remove zombie processes.
tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pktsCapture 10,000 packets destined for port 80 for later analysis.
# less pkts | awk {'printf $3"
"'} | cut -d. -f 1-4 | sort | uniq -c | awk {'printf $1" "$2"
"'} | sort -n -t\ +0Sort captured IP addresses by occurrence count. netstat -anp | grep php-cgi | grep ^tcp | wc -l Count active php-cgi processes.
chkconfig --list | awk '{if ($5=="3:on") print $1}'List services configured to start at run‑level 3. kudzu --probe --class=network Show network card model information.
Common Regular Expressions
Below is a curated list of useful regular expressions for text processing.
Match Chinese characters: [\u4e00-\u9fa5] Match double‑byte characters (including Chinese): [^\x00-\xff] Match blank lines: \n\s*\r Match HTML tags: <(\S*?)[^>]*>.*?</\1>|<.*? /> Trim leading/trailing whitespace: ^\s*|\s*$ Validate email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* Match URLs: [a-zA-Z]+://[^\s]* Validate usernames (5‑16 characters, letters, numbers, underscore, start with a letter): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ Match Chinese phone numbers: \d{3}-\d{8}|\d{4}-\d{7} Match QQ numbers (starting from 10000): [1-9][0-9]{4,} Match Chinese postal codes: [1-9]\d{5}(?!\d) Match Chinese ID numbers (15 or 18 digits): \d{15}|\d{18} Match IPv4 addresses: \d+\.\d+\.\d+\.\d+ Match positive integers: ^[1-9]\d*$ Match negative integers: ^-[1-9]\d*$ Match integers (positive or negative): ^-?[1-9]\d*$ Match non‑negative integers (including zero): ^[1-9]\d*|0$ Match non‑positive integers (including zero): ^-[1-9]\d*|0$ Match positive floating‑point numbers: ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ Match negative floating‑point numbers: ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ Match any floating‑point number: ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ Match alphabetic strings: ^[A-Za-z]+$ Match uppercase strings: ^[A-Z]+$ Match lowercase strings: ^[a-z]+$ Match alphanumeric strings: ^[A-Za-z0-9]+$ Match word characters (letters, digits, underscore):
^\w+$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.
