Operations 8 min read
17 Essential Linux Ops Tricks to Boost Your Productivity
This article compiles seventeen practical Linux administration techniques—from batch file handling and directory checks to log analysis, disk monitoring, firewall rules, and network capture—each illustrated with ready‑to‑run shell commands and concise explanations for sysadmins.
Open Source Linux
Open Source Linux
1. Move all *.tar files to a backup directory
find . -name "*.tar" -exec mv {} ./backup/ ;Use find with -exec (or xargs ) to locate files and move them; other useful options include -mtime , -type , and -size .
2. Batch unzip all *.zip files to a target folder
for i in `find . -name "*.zip" -type f`; do
unzip -d $i /data/www/img/
doneThe for … in …; do …; done loop iterates over each zip file and extracts it.
3. Common sed one‑liners (example file: test.txt)
sed -i 's/^\.//g' test.txt # remove leading dot
sed -i 's/^/a/g' test.txt # prepend "a" to each line
sed -i 's/$/a/' test.txt # append "a" to each line
sed '/wuguangke/ac' test.txt # add "c" after matching line
sed '/wuguangke/ic' test.txt # insert "c" before matching lineRefer to sed documentation for additional patterns.
4. Check if a directory exists, create it or echo a message
if [ ! -d /data/backup/ ]; then
mkdir -p /data/backup/
else
echo "The Directory already exists, please exit"
fi5. Monitor root partition usage and email when ≥90%
# Get usage percentage
usage=$(df -h | awk 'NR==2 {print $5}' | tr -d '%')
# Loop and alert
while sleep 5m; do
if [ $usage -ge 90 ]; then
echo "Root partition usage $usage% exceeds threshold" |
mail -s "Warning: Disk usage $usage%" [email protected]
fi
done6. List top 20 IPs from Nginx access log
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -207. Disable SELinux enforcement
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config8. Print maximum and minimum values from a file
cat a.txt | sort -nr | awk 'NR==1{print "max="$0} END{print "min="$0}'9. Retrieve SNMP v2c data with snmpwalk
snmpwalk -v2c -c public 192.168.0.24110. Replace lines ending with "jk" by "yz"
sed -e 's/jk$/yz/g' b.txt11. Capture network packets with tcpdump
tcpdump -nn host 192.168.56.7 and port 80
# Exclude a host
tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 8012. Show the 20 most used commands from bash history
cat .bash_history | grep -v '^#' | awk '{print $1}' | sort | uniq -c | sort -nr | head -2013. Delete *.log files older than 3 days
find . -mtime +3 -name "*.log" | xargs rm -rf14. 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 REJECT16. Nginx log statistics – top 10 IPs
cd /home/logs/nginx/default
cat access.log | awk '{print $1}' | sort -n | uniq -c | sort -nr | head -1017. Replace directory path in a file
sed 's:/user/local:/tmp:g' test.txtWritten 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
Rate this article
Was this worth your time?
Discussion
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.