Master Essential Linux Commands: Find, Sed, Iptables, and More
This guide presents a comprehensive collection of Linux command‑line techniques—including file searching, batch extraction, text processing with sed, directory checks, disk usage monitoring, log analysis, SNMP queries, firewall rules, and more—complete with ready‑to‑run examples for system administrators.
1. Find all files ending with .tar in the current directory and move them to a backup folder: 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 to specify file or directory, and -size for size filtering, e.g., find log files older than 30 days larger than 100 M and delete them.
find . -name "*.log" -mtime +30 -type f -size +100M | xargs rm -rf {};2. Batch unzip all .zip files in the current directory to a target directory:
for i in `find . -name "*.zip" -type f`
do
unzip -d $i /data/www/img/
doneNote: The for i in $(command); do … done loop iterates over command output.
3. Common sed commands (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
sed '/wuguangke/a c' test.txt
# Insert a "c" before a specific line
sed '/wuguangke/i c' test.txtRefer to the sed documentation for more patterns.
4. 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"
fi5. Monitor the root partition; if usage reaches ≥ 90 % send an email alert:
# Print root partition usage percentage
df -h | sed -n '/\/$/p' | awk '{print $5}' | awk -F '%' '{print $1}'
# Loop to check every 5 minutes and send mail when >90%
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
done6. List the top 20 IP addresses by request count from an Nginx access log:
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -207. Replace a line in /etc/selinux/config to disable SELinux enforcement:
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config8. Print the maximum and minimum values from a file:
cat a.txt | sort -nr | awk 'NR==1{print}' # maximum
cat a.txt | sort -n | awk 'NR==1{print}' # minimum9. Retrieve SNMP v2c data using snmpwalk: snmpwalk -v2c -c public 192.168.0.241 10. Replace lines ending with jk to yz in b.txt: 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 8012. Configure SNMP community name on H3C devices (set version first, then community):
snmp-agent sys-info version v1 v2c
snmp-agent community read public13. Show 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 *.log files created more than 3 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. 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 REJECTAlternative rule using state tracking:
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT17. Nginx log statistics to get the 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 -1018. Replace a directory path in a file using sed:
sed 's:/user/local:/tmp:g' test.txt
# or
sed -i 's//usr/local//tmp/g' test.txtSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
