Operations 10 min read

Essential Linux Shell Commands for Everyday System Administration

This article compiles a practical collection of Linux command‑line snippets covering file searching, batch extraction, text manipulation with sed, directory checks, disk usage monitoring, log analysis, firewall rules, and other common sysadmin tasks, each illustrated with exact command examples and brief explanations.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Essential Linux Shell Commands for Everyday System Administration

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

Find every file ending with .tar in the current directory and move it to /backup/:

find . -name "*.tar" -exec mv {} ./backup/ ;
Note: find -name searches by filename; -exec or xargs can pipe the results to another command. Common options include -mtime (modification time), -type (file type, e.g., f for regular files, d for directories), and -size (file size).

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

Iterate over every .zip file and extract it to /data/www/img/:

for i in `find . -name "*.zip" -type f`
do
    unzip -d $i /data/www/img/
 done
Note: The for i in (command); do … done pattern is a common shell loop where i holds each filename.

3. 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 's/^/a/g' test.txt Add an a at the end of each line: sed 's/$/a/' test.txt Append a c after a specific line containing the pattern wuguangke: sed '/wuguangke/ac' test.txt Insert a c before a specific line containing the pattern wuguangke: sed '/wuguangke/ic' test.txt Refer to the sed manual for additional commands.

4. 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
Explanation: ! -d tests for non‑existence; mkdir -p creates the directory hierarchy; echo prints a message.

5. Monitor root partition usage and email when ≥90%

Step 1 – Retrieve the usage percentage:

df -h | sed -n '/\/$/p' | awk '{print $5}' | awk -F'%' '{print $1}'

Step 2 – Periodically check and send an alert:

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 disk space, please check!" | mail -s "Warn Linux Disk $i%" [email protected]
        fi
    done
 done

6. List top 20 IP addresses by Nginx access log

cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20
Note: sort orders lines, uniq -c counts occurrences.

7. Disable SELinux enforcement

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

Another example replaces /tmp with /tmp/abc/ in test.txt:

sed -i 's:/tmp:/tmp/abc/:g' test.txt

8. Print maximum and minimum values from a file

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

Alternative using sed:

sed 's/ / /g' a.txt | sort -nr | sed -n '1p;$p'

9. Retrieve SNMP v2c data with snmpwalk

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 packets with tcpdump

Capture traffic to host 192.168.56.7 on port 80: tcpdump -nn host 192.168.56.7 and port 80 Capture all traffic except host 192.168.0.22 on port 80:

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

12. Show the 20 most frequently used commands

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

13. Delete *.log files older than 3 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

Alternative rule:

iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

16. Nginx log statistics – top 10 IPs

cd /home/logs/nginx/default
sort -m -k4 -o access.logok access.1 access.2 access.3 ...
cat access.logok | awk '{print $1}' | sort -n | uniq -c | sort -nr | head -10

17. Replace directory paths in a file

sed 's:/user/local:/tmp:g' test.txt

Or in‑place editing:

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.

LinuxShellScriptingSystem Administration
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.