Essential Linux Command Cheat Sheet for File Management, Monitoring, and Automation
This article compiles practical Linux shell commands for locating and moving files, batch extracting archives, using sed for text manipulation, checking directory existence, monitoring disk usage, analyzing logs, managing firewalls, and automating common sysadmin tasks, all presented with clear examples and explanations.
1. Move all *.tar files to a backup directory
Use find . -name "*.tar" -exec mv {} ./backup/ \; to locate every file ending with .tar in the current directory tree and move it to ./backup/.
2. Batch unzip all *.zip files to a target directory
Iterate over zip files with a for‑loop:
for i in $(find . -name "*.zip" -type f); do
unzip -d $i /data/www/img/
done3. Common sed one‑liners (example file: test.txt )
Remove a leading dot: sed -i 's/^\.//g' test.txt Insert an a at the beginning of each line: sed -i 's/^/a/' test.txt Append an a at the end of each line: sed -i 's/$/a/' test.txt Add a c after a specific line (example pattern wuguangke): sed -i '/wuguangke/a c' test.txt Prepend a c before a specific line:
sed -i '/wuguangke/i c' test.txt4. Test for a directory and create it if missing
Script snippet:
if [ ! -d /data/backup/ ]; then
mkdir -p /data/backup/
else
echo "The Directory already exists, please exit"
fi5. Monitor root partition usage and send an email when it exceeds 90 %
Print the usage percentage:
df -h | sed -n '/\/$/p' | awk '{print $5}' | awk -F'%' '{print $1}'Continuously check and 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 / Parts is $i%" [email protected]
fi
done
done6. List the top 20 IP addresses from an Nginx access log
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -207. Replace SELinux mode in /etc/selinux/config
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config8. Print the maximum and minimum values in a numeric file
Using sort and awk:
cat a.txt | sort -nr | awk 'NR==1{print}' # maximum
cat a.txt | sort -n | awk 'END{print}' # minimum9. Retrieve SNMP v2c data with snmpwalk
snmpwalk -v2c -c public 192.168.0.24110. Change file extensions ending with jk to yz
sed -e 's/jk$/yz/g' b.txt11. 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 particular host/port:
tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 8012. Show the 20 most frequently used shell commands
cat .bash_history | grep -v '^#' | awk '{print $1}' | sort | uniq -c | sort -nr | head -2013. 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 to allow only remote TCP traffic on 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 ACCEPT16. Nginx log analysis to list the top 10 IP addresses
cd /home/logs/nginx/default
sort -m -k4 -o access.logok access.1 access.2 access.3 ...
cat access.logok | awk '{print $1}' | sort | uniq -c | sort -nr | head -1017. Replace directory paths inside a file
sed 's:/user/local:/tmp:g' test.txtThese snippets provide a quick reference for everyday Linux system administration, file handling, text processing, monitoring, and security configuration.
Signed-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.
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.)
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.
