Operations 8 min read

Essential Linux Command-Line Tricks for System Administration

This guide compiles practical Linux shell commands and scripts for tasks such as locating and moving files, batch extraction, text manipulation with sed, directory checks, disk‑space monitoring with email alerts, log analysis, firewall rules, SNMP queries, and more, helping sysadmins automate routine operations efficiently.

ITPUB
ITPUB
ITPUB
Essential Linux Command-Line Tricks for System Administration

1. Move all *.tar files to a backup directory

Use find . -name "*.tar" -exec mv {} ./backup/ \; to locate every .tar archive in the current tree and move it to ./backup/. The -exec action processes each match immediately.

2. Batch unzip all *.zip files to a target folder

Iterate over zip files with a for loop:

for i in `find . -name "*.zip" -type f`
do
  unzip -d $i /data/www/img/
 done

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

Remove a leading dot: sed -i '/^\./d' test.txt Insert an a at the start of each line: sed -i '/^/a a' test.txt Append an a at the end of each line: sed -i '$a a' test.txt After a line containing wuguangke, add c: sed -i '/wuguangke/a c' test.txt Replace wuguangke with c on the same line:

sed -i 's/wuguangke/c/' test.txt

4. Test whether a directory exists

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

5. Monitor root partition usage and send an email when it exceeds 90 %

Extract the usage percentage: df -h | awk 'NR==2 {gsub(/%/, ""); print $5}' Continuously check every 5 minutes and mail when the threshold is crossed:

while sleep 5m
do
  for i in `df -h | awk 'NR==2 {gsub(/%/, ""); print $5}'`
  do
    if [ $i -ge 90 ]; then
      echo "More than 90% disk space used" | mail -s "Disk warning $i%" [email protected]
    fi
  done
done

6. List the top 20 IP addresses from an Nginx access log

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

7. Disable SELinux enforcing mode with sed

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

8. Print the maximum and minimum values in a file

Using sort and awk:

cat a.txt | sort -nr | awk 'END{print}'   # maximum
cat a.txt | sort -n  | awk 'END{print}'   # minimum

9. Retrieve SNMP data with version 2c

snmpwalk -v2c -c public 192.168.0.241

10. Replace lines ending with jk by yz

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

11. 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 a host:

tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80

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

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

13. Delete *.log files older than three days

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

14. Move files larger than 100 KB to /tmp

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

15. Simple firewall script allowing only remote access to port 80

iptables -F
iptables -X
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -j REJECT

16. Nginx log statistics – top 10 IPs

cd /home/logs/nginx/default
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -10

17. Replace a directory path inside a file

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.

OperationsShellSysadminScripting
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.