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.
1. Find all files ending with
.tarin 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
.zipfiles in the current directory to a specified folder:
for i in `find . -name "*.zip" -type f`
do
unzip -d $i /data/www/img/
doneNote: for i in (command); do … done is a common loop format where i is a user‑defined variable.
4. Common
sedcommands (example file
test.txt):
Remove the first character of each line:
sed -i 's/^.{1}//' test.txtAdd an
aat the beginning of each line:
sed 's/^/a/' test.txtAdd an
aat the end of each line:
sed 's/$/a/' test.txtInsert a
cafter a specific line:
sed '/pattern/a c' test.txtInsert a
cbefore a specific line:
sed '/pattern/i c' test.txt5. 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"
fi6. 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
done7. 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 -20Note: 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/config9. 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}' # min10. Use
snmpwalkto retrieve data from a device with SNMP v2c:
snmpwalk -v2c -c public 192.168.0.24111. Replace lines ending with
jkto end with
yzin a file:
sed -e 's/jk$/yz/g' b.txt12. Capture network packets with
tcpdumpfor a specific host and port:
tcpdump -nn host 192.168.56.7 and port 8013. List the 20 most frequently used commands from Bash history:
cat .bash_history | grep -v '^#' | awk '{print $1}' | sort | uniq -c | sort -nr | head -2014. Find
.logfiles 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 REJECTAlternative rule using state matching:
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT17. 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 -10Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.