The Power of the Single Line

One of Bash's superpowers is the ability to accomplish complex tasks in a single command by chaining tools together with pipes. These aren't just party tricks — the following one-liners solve real problems developers encounter regularly. Bookmark this page and come back to it often.

1. Find and Kill a Process by Port

kill -9 $(lsof -ti:3000)

Replace 3000 with any port number. Instantly kills whatever process is occupying that port — essential when your dev server refuses to restart.

2. Batch Rename Files with a Pattern

for f in *.txt; do mv "$f" "${f%.txt}.md"; done

Renames all .txt files in the current directory to .md. The ${f%.txt} syntax strips the old extension — works for any extension pair.

3. Find All TODO Comments in a Codebase

grep -rn "TODO" --include="*.py" .

Recursively searches all Python files for TODO comments, showing the filename and line number. Swap *.py for *.js, *.ts, or any file type.

4. Monitor a Log File in Real Time

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

Streams log output in real time, but only shows lines containing "ERROR". Invaluable when debugging running services.

5. Show Disk Usage, Sorted by Size

du -sh * | sort -rh | head -20

Lists the 20 largest files and folders in the current directory, sorted from largest to smallest. Use this when you need to find what's eating your disk space.

6. Extract a Specific Column from a CSV

cut -d',' -f3 data.csv | sort | uniq -c | sort -rn

Extracts column 3, sorts the values, counts unique occurrences, and sorts by frequency. Change -f3 for a different column. Great for quick data analysis without opening Excel.

7. Create a Timestamped Backup of a File

cp config.yaml config.yaml.$(date +%Y%m%d_%H%M%S).bak

Creates a backup with a timestamp embedded in the filename. Run this before editing any important configuration file.

8. Download Multiple URLs from a List

xargs -n1 curl -O < urls.txt

Reads a list of URLs from urls.txt (one per line) and downloads each one with curl. Much faster than downloading them manually one by one.

9. Check if a Website is Up

curl -o /dev/null -s -w "%{http_code}" https://example.com

Prints just the HTTP status code (e.g., 200 or 503) for a given URL. Wrap this in a loop to monitor multiple sites from a list.

10. Count Lines of Code by File Type

find . -name "*.py" | xargs wc -l | sort -rn | tail -1

Finds all Python files and prints the total line count. Replace *.py with any extension. Remove | tail -1 to see per-file counts.

Building Your Own One-Liner Toolkit

The best way to internalize these is to keep a personal "cheat sheet" — a plain text file or a notes app where you paste commands that solve problems you've actually encountered. Over time, this becomes one of your most valuable resources. You'll find yourself reaching for the same patterns again and again, and eventually you'll start combining them in new ways automatically.