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/

:

<code>find . -name "*.tar" -exec mv {} ./backup/ ;</code>

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:

<code>find . -name "*.log" -mtime +30 -type f -size +100M | xargs rm -rf {} ;</code>

3. Batch unzip all .zip files in the current directory

Iterate over each

.zip

file and extract it to a target directory:

<code>for i in $(find . -name "*.zip" -type f); do
    unzip -d $i /data/www/img/
 done</code>

4. Common sed one‑liners (example file: test.txt )

Remove the first character of each line:

<code>sed -i 's/^\.//g' test.txt</code>

Add an

a

at the beginning of each line:

<code>sed -i 's/^/a/' test.txt</code>

Add an

a

at the end of each line:

<code>sed -i 's/$/a/' test.txt</code>

Append a

c

after a specific line (e.g., line containing "wuguangke"):

<code>sed '/wuguangke/a c' test.txt</code>

Insert a

c

before a specific line:

<code>sed '/wuguangke/i c' test.txt</code>

5. Check if a directory exists, create it if not

<code>if [ ! -d /data/backup/ ]; then
    mkdir -p /data/backup/
else
    echo "The directory already exists, please exit"
fi</code>

6. Monitor root partition usage and alert when ≥90%

Print the usage percentage and send an email warning if it exceeds 90%:

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

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

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

8. Disable SELinux enforcing mode

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

9. Print the maximum and minimum values from a file

<code>cat a.txt | sort -nr | awk 'NR==1{print "max="$0} END{print "min="$0}'</code>

10. Retrieve SNMP data (v2c) from a host

<code>snmpwalk -v2c -c public 192.168.0.241</code>

11. Replace lines ending with jk by yz

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

12. Capture network traffic with tcpdump

Capture HTTP traffic from a specific host:

<code>tcpdump -nn host 192.168.56.7 and port 80</code>

Capture all traffic except from a given host:

<code>tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80</code>

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

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

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

<code>find . -mtime +3 -name "*.log" | xargs rm -rf</code>

15. Move files larger than 100 KB to /tmp

<code>find . -size +100k -exec mv {} /tmp \;</code>

16. Simple firewall script allowing only port 80

<code>iptables -F
iptables -X
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -j REJECT</code>

17. Nginx log aggregation and top‑10 IP statistics

Merge split logs, then count the most frequent IPs:

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

18. Replace a directory path in a file using sed

<code>sed -i 's:/usr/local:/tmp:g' test.txt</code>
AutomationlinuxCommand 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

login 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.