Master Essential Linux Commands: Find, Sed, Disk Monitoring, and More
This guide presents a collection of practical Linux command‑line techniques—including file searching, batch extraction, text manipulation with sed, directory checks, disk usage monitoring, log analysis, firewall rules, and SNMP queries—to help system administrators automate routine tasks efficiently.
1. Move all .tar files to a backup directory
Find files ending with .tar in the current directory and move them to ./backup/:
find . -name "*.tar" -exec mv {} ./backup/ ;2. Delete large log files older than 30 days
Locate .log files modified more than 30 days ago and larger than 100 MB, then remove them:
find . -name "*.log" -mtime +30 -type f -size +100M | xargs rm -rf {} ;3. Batch unzip all .zip files in the current directory
Iterate over each .zip file and extract it to a target directory:
for i in $(find . -name "*.zip" -type f); do
unzip -d $i /data/www/img/
done4. Common sed one‑liners (example file: test.txt )
Remove the first character of each line: sed -i 's/^\.//g' test.txt Add an a at the beginning of each line: sed -i 's/^/a/' test.txt Add an a at the end of each line: sed -i 's/$/a/' test.txt Append a c after a specific line (e.g., line containing "wuguangke"): sed '/wuguangke/a c' test.txt Insert a c before a specific line:
sed '/wuguangke/i c' test.txt5. Check if a directory exists, create it if not
if [ ! -d /data/backup/ ]; then
mkdir -p /data/backup/
else
echo "The directory already exists, please exit"
fi6. Monitor root partition usage and alert when ≥90%
Print the usage percentage and send an email warning if it exceeds 90%:
while sleep 5m; do
for i in $(df -h | awk 'NR>1 {print $5}' | sed 's/%//g'); do
if [ $i -ge 90 ]; then
echo "Root partition usage is $i% – please check!" | mail -s "Disk Space Warning $i%" [email protected]
fi
done
done7. List the top 20 IP addresses in an Nginx access log
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -208. Disable SELinux enforcing mode
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config9. Print the maximum and minimum values from a file
cat a.txt | sort -nr | awk 'NR==1{print "max="$0} END{print "min="$0}'10. Retrieve SNMP data (v2c) from a host
snmpwalk -v2c -c public 192.168.0.24111. Replace lines ending with jk by yz
sed -e 's/jk$/yz/g' b.txt12. Capture network traffic with tcpdump
Capture HTTP traffic from a specific host: tcpdump -nn host 192.168.56.7 and port 80 Capture all traffic except from a given host:
tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 8013. Show the most frequently used 20 commands from Bash history
cat .bash_history | grep -v '^#' | awk '{print $1}' | sort | uniq -c | sort -nr | head -2014. Delete .log files created more than 3 days ago
find . -mtime +3 -name "*.log" | xargs rm -rf15. Move files larger than 100 KB to /tmp
find . -size +100k -exec mv {} /tmp \;16. Simple firewall script allowing only port 80
iptables -F
iptables -X
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -j REJECT17. Nginx log aggregation and top‑10 IP statistics
Merge split logs, then count the most frequent IPs:
cd /home/logs/nginx/default
sort -m -k 4 -o access.log.ok access.1 access.2 access.3 ...
cat access.log.ok | awk '{print $1}' | sort -n | uniq -c | sort -nr | head -1018. Replace a directory path in a file using sed
sed -i 's:/usr/local:/tmp:g' test.txtSigned-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.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
