Operations 9 min read

Master Essential Linux Shell Commands for File Management, Monitoring, and Automation

This guide presents a collection of practical Linux shell commands and scripts for locating and moving files, batch extracting archives, using sed for text manipulation, checking directories, monitoring disk usage, analyzing logs, and configuring firewalls, all illustrated with clear examples and explanations.

Open Source Linux
Open Source Linux
Open Source Linux
Master Essential Linux Shell Commands for File Management, Monitoring, and Automation

1. Find all files ending with

.tar

in the current directory and move them to a backup directory:

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

2. Find and delete log files older than 30 days and larger than 100 MB:

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

3. Batch unzip all

.zip

files in the current directory to a specified folder:

for i in `find . -name "*.zip" -type f`
do
  unzip -d $i /data/www/img/
 done
Note: for i in (command); do … done is a common loop format where i is a user‑defined variable.

4. Common

sed

commands (example file

test.txt

):

Remove the first character of each line:

sed -i 's/^.{1}//' test.txt

Add an

a

at the beginning of each line:

sed 's/^/a/' test.txt

Add an

a

at the end of each line:

sed 's/$/a/' test.txt

Insert a

c

after a specific line:

sed '/pattern/a c' test.txt

Insert a

c

before a specific line:

sed '/pattern/i c' test.txt

5. Check if a directory exists; create it if not, otherwise print a message:

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

6. Monitor root partition usage; if usage reaches 90% or more, send an email alert:

# Print root partition usage percentage
usage=$(df -h | awk 'NR==2 {print $5}' | tr -d '%')
while sleep 5m; do
  if [ $usage -ge 90 ]; then
    echo "More than 90% disk space used on Linux, please check!" | mail -s "Disk Warning $usage%" [email protected]
  fi
done

7. Show the top 20 IP addresses by request count in an Nginx access log:

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

8. Replace SELinux enforcing mode with disabled in

/etc/selinux/config

:

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
cat a.txt | sort -n  | awk 'END{print}'   # min

10. Use

snmpwalk

to retrieve data from a device with SNMP v2c:

snmpwalk -v2c -c public 192.168.0.241

11. Replace lines ending with

jk

to end with

yz

in a file:

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

12. Capture network packets with

tcpdump

for a specific host and port:

tcpdump -nn host 192.168.56.7 and port 80

13. List the 20 most frequently used commands from Bash history:

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

14. Find

.log

files created more than three days ago and delete them:

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

15. Move files larger than 100 KB from a directory to

/tmp

:

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

16. Configure a firewall to allow 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 using state matching:

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

17. Generate Nginx log statistics to find the top 10 IP addresses:

cd /home/logs/nginx/default
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -10
monitoringautomationLinuxshellScriptingbashFile Management
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.