The command line is a developer’s best friend, and bash one-liners are the Swiss Army knife of system administration and development workflows. These compact commands can replace entire scripts, automate repetitive tasks, and solve complex problems in a single line.

max@narsil:~$ cowsay "Welcome to the world of bash one-liners!"
 _________________________________________
< Welcome to the world of bash one-liners! >
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Here’s a curated collection of bash one-liners that I’ve found invaluable over the years, organised by use case with explanations of how they work.

File and Directory Operations Link to heading

Find largest files in a directory Link to heading

du -ah . | sort -rh | head -n 10

du shows disk usage, -ah . displays all files and folders in human-readable format, sort -rh sorts by size in reverse order, and head -n 10 shows the top 10 results.

Show disk usage in a tree-like fashion Link to heading

du -h --max-depth=1 | sort -hr

See which folders eat up the most space at the current directory level.

Create timestamped backup archive Link to heading

tar -czf backup_$(date +%Y%m%d_%H%M%S).tar.gz /path/to/folder

Creates a compressed backup with a timestamp in the filename for easy identification and organisation.

Text Processing and Data Manipulation Link to heading

Replace text in multiple files Link to heading

grep -rl 'oldText' ./ | xargs sed -i 's/oldText/newText/g'

grep -rl finds all files containing oldText (recursive, list filenames only), then xargs passes those filenames to sed -i which replaces the text in-place across all found files.

Remove duplicate lines from a file Link to heading

awk '!a[$0]++' file.txt

Uses awk’s associative arrays to track seen lines and only print unique ones. Preserves the order of first occurrence.1

System Monitoring and Process Management Link to heading

Show top memory-consuming processes Link to heading

ps aux --sort=-%mem | head -n 10

Similar to top, but as a one-liner snapshot showing the 10 most memory-intensive processes using ps.

Monitor real-time log changes Link to heading

tail -f /var/log/syslog | grep --line-buffered "error"

tail -f follows a log file and grep --line-buffered shows lines with “error” as new lines are appended to the log file.

Development Workflow Link to heading

Random password generator Link to heading

openssl rand -base64 12

Generates a secure random password using OpenSSL. The -base64 flag ensures the output is printable characters.

Save file with sudo when you forgot to open with sudo Link to heading

:w !sudo tee %

Not strictly a bash one-liner but I use this Vim command all the time - it allows you to save a file with sudo privileges if you’ve forgotten to open it with sudo.2

Fun and Dangerous Link to heading

Fork bomb (DO NOT RUN) Link to heading

:(){ :|:& };:

A classic example of a fork bomb that creates processes exponentially until the system becomes unresponsive. Included here for educational purposes only, while the modern Linux kernel has safeguards against attacks like these you should still never run this on any system you care about! This command was the cause of much hilarity/annoyance (depending on who you ask) in my youth.3

Tips for Writing Effective One-Liners Link to heading

  1. Use pipes wisely: Chain commands together, but keep readability in mind
  2. Test with small datasets: Always verify your one-liner works on a subset before running on production data
  3. Escape special characters: Use quotes and backslashes appropriately
  4. Consider performance: Some operations can be resource-intensive on large datasets
  5. Document complex ones: Add comments or save frequently-used one-liners as shell functions

Building Your Own Arsenal Link to heading

The best bash one-liners are often domain-specific. Start by identifying repetitive tasks in your workflow, then research the appropriate command combinations. Tools like awk, sed, grep, find, and xargs are the building blocks of most powerful one-liners.

For detailed information about any command’s options and usage, use man command (e.g., man grep, man awk) to access the manual pages.

Remember: the goal isn’t to write the most obscure command possible, but to create efficient, readable solutions that save time and reduce errors in your daily work.


  1. This awk technique is more memory-efficient than sort | uniq for large files since it doesn’t need to sort the entire dataset first. ↩︎

  2. The tee command writes to both stdout and a file, so sudo tee % writes the buffer content to the current file with elevated privileges while also displaying it. ↩︎

  3. Fork bombs exploit the way Unix-like systems handle process creation. Each : function calls itself twice, creating an exponential explosion of processes that quickly exhausts system resources. ↩︎