17 Must-Know Linux Ops Tricks for Sysadmins: From Log Analysis to Disk Monitoring
This article presents 17 practical Linux command-line techniques for system administrators, covering file search and manipulation with find and sed, log analysis using awk and sort, disk space monitoring with automated alerts, network packet capture via tcpdump, firewall configuration with iptables, and shell scripting for routine maintenance tasks.
This guide shares 17 essential Linux command-line techniques that every operations engineer should master. Each tip includes a concrete command example and a brief annotation explaining the key flags and logic.
1. Find and move .tar files to a backup directory
Use find with -name and -exec to locate all .tar files in the current directory and move them to ./backup/.
find . -name “*.tar” -exec mv {} ./backup/ \;Annotation: find -name matches filenames; -exec or xargs passes results to a subsequent command. Extensions: -mtime for modification time, -type for file type (f=file, d=directory), -size for size. Example: delete .log files older than 30 days and larger than 100 MB:
find . -name “*.log” –mtime +30 –type f –size +100M | xargs rm –rf {};2. Batch unzip all .zip files to a target directory
A for loop iterates over find results and extracts each archive with unzip -d.
for i in `find . –name “*.zip”–type f `
do
unzip –d $i /data/www/img/
doneAnnotation: for i in (command); do ... done is a common loop pattern; the variable name is arbitrary.
3. Common sed one-liners (tested on test.txt)
Remove leading dot from each line: sed -i ‘s/^.//g’ test.txt Prepend 'a' to every line: sed ‘s/^/a/g’ test.txt Append 'a' to every line: sed ‘s/$/a/’ test.txt Insert 'c' after lines matching 'wuguangke': sed ‘/wuguangke/ac’ test.txt Insert 'c' before lines matching 'wuguangke': sed ‘/wuguangke/ic’ test.txt Refer to sed documentation for more commands.
4. Check if a directory exists; create it if missing, else print a message
if
[! –d /data/backup/];
then
Mkdir–p /data/backup/
else
echo “The Directory alreadyexists,please exit”
fiAnnotation: if...;then ...else ..fi is the conditional structure; ! negates the test, -d checks for a directory.
5. Monitor root filesystem usage; email alert when usage ≥ 90%
Step 1: Extract the root partition usage percentage:
df -h |sed -n ‘//$/p’|awk ‘{print $5}’|awk –F “%” ‘{print $1}’Annotation: awk '{print $5}' prints the 5th field; -F</u201d%” splits on '%' to drop the percent sign.
Step 2: Loop every 5 minutes, check each filesystem, and send mail if usage ≥ 90:
while sleep 5m
do
for i in `df -h |sed -n ‘//$/p’ |awk ‘{print $5}’ |sed ‘s/%//g’`
do
echo $i
if [ $i -ge 90 ];
then
echo “More than 90% Linux of disk space ,Please LinuxSA Check Linux Disk !” |mail -s “Warn Linux / Parts is $i%” [email protected]
fi
done
done6. Top 20 client IPs from Nginx access log
cat access.log |awk ‘{print $1}’|sort|uniq -c |sort -nr |head -20Annotation: sort orders lines; uniq -c counts duplicate lines.
7. sed pattern: match a line and replace a parameter on that line
sed -i ‘/SELINUX/s/enforcing/disabled/’ /etc/selinux/configAlternative delimiter (colon) to avoid escaping slashes: sed -i ‘s:/tmp:/tmp/abc/:g’ test.txt replaces /tmp with /tmp/abc/.
8. Print the maximum and minimum values from a numeric file
First attempt (flawed):
cat a.txt |sort -nr|awk ‘{}END{print} NR==1’
cat a.txt |sort -nr |awk ‘END{print} NR==1’Correct approach using sed to normalize spaces, then sort -nr and print first and last lines:
sed ‘s/ / /g’ a.txt |sort -nr|sed -n ‘1p;$p’9. SNMP v2c walk for Cacti data collection
snmpwalk -v2c -c public 192.168.0.24110. Replace lines ending with 'jk' to 'yz'
sed -e ‘s/jk$/yz/g’ b.txt11. Network packet capture with tcpdump
Capture packets from host 192.168.56.7 on port 80: tcpdump -nn host 192.168.56.7 and port 80 Exclude host 192.168.0.22 on port 80:
tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80Reference: TCP/IP 7-layer model (Physical – Data Link – Network – Transport – Session – Presentation – Application).
12. Show the 20 most frequently used commands from history
cat .bash_history | grep -v ^# | awk ‘{print $1}’ | sort | uniq -c | sort -nr | head-2013. Script: delete *.log files older than 3 days
find . -mtime +3 –name “*.log” |xargs rm -rf {} ;14. Script: move files larger than 100k to /tmp
find . -size +100k -exec mv {} /tmp ;15. Firewall script: allow only remote access to port 80
Option A (flush and set default reject):
iptables -F
iptables -X
iptables -A INPUT -p tcp --dport 80 -j accept
iptables -A INPUT -p tcp -j REJECTOption B (stateful rule for new connections on port 80):
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT16. Nginx log analysis: top 10 client IPs (log path: /home/logs/nginx/default/access.log)
cd /home/logs.nginx/default
sort -m -k 4 -o access.logok access.1 access.2 access.3 .....
cat access.logok |awk ‘{print $1}’|sort -n|uniq -c|sort -nr |head -1017. Replace directory paths in a file
Using colon delimiter: sed ‘s:/user/local:/tmp:g’ test.txt Or with escaped slashes:
sed -i ‘s//usr/local//tmp/g’ test.txtSource: 浩道linux
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.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.
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.
