20 Essential Linux Command-Line Tricks Every Sysadmin Should Know
A concise collection of practical Linux shell commands covers file clearing, log analysis, bulk file creation, mass file operations, advanced find usages, history shortcuts, sed editing tricks, network interface filtering, and more, providing sysadmins with ready-to-use shortcuts for daily tasks.
Linux Knowledge Pocket
This article gathers a set of handy Linux command‑line tricks that can speed up everyday system administration work.
1. Quickly empty a file
cat /dev/null > haodao.txt2. Count unique IP addresses in a Tomcat access log
awk '{print $1}' access.log | sort | uniq | wc -l3. Generate a large test file
Use dd to create a file of a specific size for disk‑read/write testing. dd if=/dev/zero of=test.txt bs=1M count=1000 The command above creates test.txt with a size of 1 GB.
4. Create many files with similar names
touch haodao{1..100}.txt5. Find and delete all .txt files in the current directory
find . -name "*.txt" -exec rm -rf {} \;Alternative forms:
find . -name "*.txt" | xargs rm -rf rm -rf $(find . -name "*.txt")6. Delete .txt files older than 7 days
find . -mtime +7 -name "*.txt" | xargs rm -rf7. Move files larger than 1000 M to /root/home
find . -size +1000M -exec mv {} /root/home \;8. Delete .txt files older than 30 days and larger than 1000 M
find . -name "*.txt" -mtime +30 -type f -size +1000M | xargs rm -rf9. Show the 10 most frequently used commands from history
cat /root/.bash_history | grep -v ^# | awk '{print $1}' | sort | uniq -c | sort -nr | head -1010. Count IP accesses to Tomcat on a specific date and hour (e.g., 2021‑12‑11 09:00)
awk '{print $4,$1}' access.log | grep '11/Dec/2021:09' | awk '{print $2}' | sort | uniq | wc -lProviding the log file allows the calculation; the same method works for any timestamp.
11. List the top 10 IP addresses by request count
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -1012. Count how many times a specific page was accessed
grep "/portal/index.html" access.log | wc -l13. Show which pages a particular IP accessed
grep ^192.168.20.100 access.log | awk '{print $1,$7}'14. Use tcpdump to inspect traffic on port 8080
tcpdump -i ens120 -tnn dst port 8080 -c 1000 | awk -F "." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr | head -1015. History command shortcuts
!!– repeat the previous command. !N – repeat command number N from history. !awk – repeat the most recent command starting with awk. !$ – use the last argument of the previous command.
Example: mv !$ !$ .bak renames test.txt to test.txt.bak.
16. Replace all occurrences of "haodao" with "HAODAO" in test.txt
sed -i "s/haodao/HAODAO/g" test.txt17. Replace a directory path in haodao.txt
sed -i "s:/etc/dhcp:/home:g" haodao.txt18. Common sed tricks on haodao.txt
Remove leading # characters: sed -i "s/^#//g" haodao.txt Add the word linux at the beginning of each line: sed -i "s/^/linux/g" haodao.txt Append study at the end of each line: sed -i "s/$/study/" haodao.txt After a line containing we love, insert you: sed -i "/we love/a you" haodao.txt Before a line containing haodao, insert where:
sed -i "/haodao/i where" haodao.txt19. List network interfaces that are up
ip addr | awk -F ':' '/state UP/ {print $2}'20. Show IP addresses and their corresponding interfaces
ip -o addr | awk '/inet/ {print $2,$4}'Conclusion
The commands above are simple yet powerful tricks that many developers overlook; mastering them can greatly improve daily productivity.
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.
