The difference between a beginner and an expert Bash user is efficiency. The best Bash users don’t waste time typing long-winded commands—they use powerful one-liners to get things done in seconds.
In this guide, you’ll find 25 practical Bash one-liners that solve real-world problems. Whether you’re managing files, troubleshooting servers, or processing logs, these commands will help you work smarter, not harder.
Want a structured, offline reference with bonus automation scripts and troubleshooting tips?
👉 Get the Complete Bash Cheat Sheet for $3.99
1. File and Directory Management
1. Quickly backup a file before editing
cp file.txt{,.bak}
Why it’s useful: Ever made a change and regretted it? This creates file.txt.bak
instantly before editing.
2. Find and delete files older than 30 days
find /path/to/dir -type f -mtime +30 -delete
Real-world use case: Useful for log rotation or cleaning up old backups.
3. Rename all .txt
files to .log
for file in *.txt; do mv "$file" "${file%.txt}.log"; done
Why it matters: Automates batch renaming instead of renaming files manually.
4. Create a directory structure in one command
mkdir -p project/{src,bin,logs,config}
How this helps: Perfect for setting up project directories quickly.
5. Find the largest files in a directory
du -ah /path/to/dir | sort -rh | head -10
When to use: Great for troubleshooting full disks in Linux servers.
2. Text Processing & Log Analysis
6. Count occurrences of "error" in a log file
grep -o "error" logfile.txt | wc -l
Real-world scenario: Helps quickly check for recurring errors in server logs.
7. Extract only email addresses from a file
grep -E -o "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}" file.txt
Why it’s useful: Saves time when scraping contact emails from logs or files.
8. Convert all text to lowercase
tr '[:upper:]' '[:lower:]' < file.txt
Why you need it: Useful when processing case-insensitive data.
9. Find duplicate lines in a file
sort file.txt | uniq -d
When to use it: Helps detect repeated log entries.
10. Extract specific columns from a CSV file
awk -F, '{print $1, $3}' data.csv
Real-world application: If you have a CSV file with 5+ columns and only need two, this keeps it clean.
3. System Monitoring & Debugging
11. Check CPU usage per process
ps -eo pid,comm,%cpu --sort=-%cpu | head -10
Why it matters: Helps troubleshoot high CPU usage.
12. Monitor memory usage in real-time
watch -n 1 free -m
Real-world scenario: Essential for diagnosing out-of-memory issues.
13. Check which process is using a specific port
netstat -tulnp | grep ":80"
When to use: Useful when Apache or Nginx isn’t starting due to port conflicts.
14. Find recently modified files
find . -type f -mtime -1
Why it helps: Instantly detect recently changed files in case of a suspected security issue.
15. View last 100 lines of a log file in real-time
tail -n 100 -f /var/log/syslog
Why it’s critical: Helps catch system errors as they happen.
4. Automation & Scheduling
16. Run a command every 10 seconds
watch -n 10 "date"
Real-world scenario: Useful for monitoring time-sensitive tasks.
17. Automatically restart a service if it crashes
while true; do systemctl is-active --quiet myservice || systemctl restart myservice; sleep 60; done
Why it’s powerful: Automates uptime monitoring.
18. Run a script at 3 AM daily
echo "0 3 * * * /path/to/script.sh" | crontab -
Use case: Perfect for backup or maintenance scripts.
5. Networking & Security
21. Check your public IP address
curl -s https://ifconfig.me
22. Test if a website is reachable
curl -Is https://example.com | head -n 1
23. Scan open ports on a remote server
nmap -p 1-65535 example.com
24. Block an IP address using iptables
iptables -A INPUT -s 192.168.1.100 -j DROP
25. Find all failed SSH login attempts
grep "Failed password" /var/log/auth.log
Final Thoughts: From One-Liners to Mastering Bash
These Bash one-liners are just a glimpse of what’s possible. A true Bash expert knows when and why to use them effectively.
🚀 Master Bash Faster with This Cheat Book!
Want to boost your productivity and avoid Googling the same Bash commands over and over? My Bash Scripting Cheat Book is the ultimate quick-reference guide for everyday tasks like:
- File handling, process management, and networking
- Regex, text manipulation, and troubleshooting techniques
-
Essential Bash utilities (
jq
,find
,grep
,awk
) explained concisely
👉 Get the Bash Cheat Sheet for just $3.99
What’s Inside?
✔️ Structured reference of 100+ essential Bash commands
✔️ Clear explanations with practical use cases
✔️ Time-saving Bash tricks you won’t find in typical man pages
✔️ Formatted PDF for easy offline use
Stop wasting time searching for commands. Keep this cheat sheet handy and level up your Bash skills today!