Operations 9 min read

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.

Efficient Ops
Efficient Ops
Efficient Ops
Master Essential Linux Commands: Find, Sed, Disk Monitoring, and More

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/
 done

4. 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.txt

5. 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"
fi

6. 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
 done

7. List the top 20 IP addresses in an Nginx access log

cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20

8. Disable SELinux enforcing mode

sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

9. 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.241

11. Replace lines ending with jk by yz

sed -e 's/jk$/yz/g' b.txt

12. 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 80

13. Show the most frequently used 20 commands from Bash history

cat .bash_history | grep -v '^#' | awk '{print $1}' | sort | uniq -c | sort -nr | head -20

14. Delete .log files created more than 3 days ago

find . -mtime +3 -name "*.log" | xargs rm -rf

15. 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 REJECT

17. 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 -10

18. Replace a directory path in a file using sed

sed -i 's:/usr/local:/tmp:g' test.txt
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

automationcommand-lineSystem AdministrationShell scripting
Efficient Ops
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.