25 Bash One-Liners That Will Make You Look Like a Pro

25 Bash One-Liners That Will Make You Look Like a Pro

Publish Date: Mar 3
0 0

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}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Why you need it: Useful when processing case-insensitive data.

9. Find duplicate lines in a file

sort file.txt | uniq -d
Enter fullscreen mode Exit fullscreen mode

When to use it: Helps detect repeated log entries.

10. Extract specific columns from a CSV file

awk -F, '{print $1, $3}' data.csv
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Why it matters: Helps troubleshoot high CPU usage.

12. Monitor memory usage in real-time

watch -n 1 free -m
Enter fullscreen mode Exit fullscreen mode

Real-world scenario: Essential for diagnosing out-of-memory issues.

13. Check which process is using a specific port

netstat -tulnp | grep ":80"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Why it’s powerful: Automates uptime monitoring.

18. Run a script at 3 AM daily

echo "0 3 * * * /path/to/script.sh" | crontab -
Enter fullscreen mode Exit fullscreen mode

Use case: Perfect for backup or maintenance scripts.


5. Networking & Security

21. Check your public IP address

curl -s https://ifconfig.me
Enter fullscreen mode Exit fullscreen mode

22. Test if a website is reachable

curl -Is https://example.com | head -n 1
Enter fullscreen mode Exit fullscreen mode

23. Scan open ports on a remote server

nmap -p 1-65535 example.com
Enter fullscreen mode Exit fullscreen mode

24. Block an IP address using iptables

iptables -A INPUT -s 192.168.1.100 -j DROP
Enter fullscreen mode Exit fullscreen mode

25. Find all failed SSH login attempts

grep "Failed password" /var/log/auth.log
Enter fullscreen mode Exit fullscreen mode

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!

Comments 0 total

    Add comment