I got tired of re-searching the same bash one-liners every time I sat down at a terminal. So I started collecting them. This is that collection — real scripts, explained like a human wrote them.
// the good stuff
Every snippet runs on Ubuntu, Debian, Fedora, and macOS. Tested personally. The comment at the top of each block tells you what it does before you run it.
#!/bin/bash # Copies a folder to /backup with today's timestamp. # Run manually or schedule with cron — works either way. SOURCE="/home/user/documents" DEST="/backup" DATE=$(date +%Y-%m-%d_%H-%M) mkdir -p "$DEST" cp -r "$SOURCE" "$DEST/backup_$DATE" echo "Done. Saved to: backup_$DATE"
#!/bin/bash # Renames every .txt file to .md — useful after # exporting notes, docs, or data dumps. for file in *.txt; do [ -f "$file" ] || continue newname="${file%.txt}.md" mv "$file" "$newname" echo "Renamed: $file → $newname" done
#!/bin/bash # Checks disk usage and warns if over 80%. # Great for daily cron on a server or VPS. THRESHOLD=80 USAGE=$(df / | awk 'NR==2{print $5}' | tr -d '%') if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "⚠ Disk at ${USAGE}% — time to clean up" else echo "✓ Disk OK: ${USAGE}% used" fi
#!/bin/bash # Deletes .log files older than 30 days. # Replace -delete with -print to preview first! LOG_DIR="/var/log/myapp" DAYS=30 find "$LOG_DIR" -type f -name "*.log" \ -mtime +$DAYS -delete echo "Cleaned logs older than $DAYS days"
#!/bin/bash # Prints key stats at a glance. Alias to 'syscheck' # in your .bashrc for fast access. echo "=== Quick System Check ===" echo "Host : $(hostname)" echo "Uptime : $(uptime -p)" echo "RAM : $(free -h | awk '/Mem/{print $3"/"$2}')" echo "Disk / : $(df -h / | awk 'NR==2{print $3"/"$2}')" echo "IP : $(hostname -I | awk '{print $1}')" echo "========================="
#!/bin/bash # Creates a folder named with today's date in ~/work. # I run this as my very first command every morning. BASE="$HOME/work" TODAY=$(date +%Y-%m-%d) DIR="$BASE/$TODAY" mkdir -p "$DIR" echo "Ready. Working in: $DIR" # xdg-open "$DIR" # uncomment to open in file manager
#!/bin/bash # Pings a URL and reports if it's reachable. # Use with cron to alert you when a site goes down. URL="https://example.com" STATUS=$(curl -o /dev/null -s -w "%{http_code}" "$URL") if [ "$STATUS" -eq 200 ]; then echo "✓ $URL is up (HTTP $STATUS)" else echo "✗ $URL returned HTTP $STATUS — check it" fi
#!/bin/bash # Searches all .txt files in a folder for a keyword. # Shows filename and line number for each match. SEARCH_DIR="$HOME/documents" KEYWORD="TODO" echo "Searching for '$KEYWORD'..." grep -rn "$KEYWORD" "$SEARCH_DIR" --include="*.txt" echo "Done."
#!/bin/bash # Counts total lines across all .py files in a project. # Quick LOC estimate without installing any extra tools. PROJECT_DIR="$HOME/myproject" EXT="py" TOTAL=$(find "$PROJECT_DIR" -name "*.$EXT" \ | xargs wc -l 2>/dev/null \ | tail -1 | awk '{print $1}') echo "Total lines of .$EXT code: $TOTAL"
#!/bin/bash # Finds files with identical content using MD5 hashes. # Run on Downloads folder to surface duplicates. TARGET="$HOME/Downloads" echo "Scanning for duplicates in $TARGET..." find "$TARGET" -type f \ | xargs md5sum 2>/dev/null \ | sort \ | awk 'seen[$1]++'
#!/bin/bash # Compresses a folder into a dated .tar.gz archive. # Smaller than .zip and native to every Linux system. SOURCE="/home/user/project" OUTPUT="/backup/project_$(date +%Y%m%d).tar.gz" tar -czf "$OUTPUT" -C "$(dirname $SOURCE)" \ "$(basename $SOURCE)" echo "Archive created: $OUTPUT" ls -lh "$OUTPUT"
#!/bin/bash # Safe bash template — use this as your starting point # for any script you actually care about. set -euo pipefail # exit on error, unset vars, pipe fails IFS=$'\n\t' # safer word splitting # Your code starts here echo "Script started at $(date)" # ... add your logic ... echo "Script completed successfully"
// interactive tool
Not sure where to start? Pick what you want to do and this generates a working starter script. No coding required — just change the paths to match your system.
⚡ Generate a Script
Choose a task and a script generates instantly. Save as a .sh file, run chmod +x yourscript.sh, then ./yourscript.sh.
// go deeper
Resources I'd actually recommend if you want to understand bash properly. All links are free unless noted.
// free tool
Visual cron schedule builder. Describe your schedule and it generates the cron syntax. I use it every time — even after years of writing cron jobs.
crontab.guru →// free tool
Paste any bash command and it breaks down every flag and argument with man page documentation. Best way to understand commands you found online.
explainshell.com →// free course
Full beginner bash course on YouTube. About 5 hours, covers variables, loops, functions, and real-world examples. Free, no account needed.
Watch on YouTube →// free tool
Paste any bash script and it finds bugs, bad practices, and portability issues. Like a spell-checker for shell scripts. Catches things that are hard to spot yourself.
shellcheck.net →// community
Active community from beginners to experts. Good for questions when you're stuck on something specific. Friendlier than Stack Overflow for scripting questions.
reddit.com/r/bash →// reference
The official, complete bash documentation. Dense but definitive — every feature, every edge case. Bookmark it and use Ctrl+F when you need the real answer.
gnu.org/bash →// more coming
Adding new snippets regularly. Each topic gets its own set of working examples with plain-English explanations.
// common questions
Questions I see constantly in forums, Discord servers, and comment sections — answered as plainly as possible.
.sh and the very first line is always #!/bin/bash — that's the computer's way of knowing which language to use.chmod +x yourscript.sh. Then run it: ./yourscript.sh. If you skip chmod, you get a "Permission denied" error — the most common beginner trip-up. You can also skip chmod entirely and type bash yourscript.sh — same result.bash --version to confirm. On Windows, install Git Bash (free, 2 minutes) or enable WSL from Windows Settings. Both give you a full bash environment at zero cost.sh is the old minimal POSIX shell — works everywhere but has fewer features. bash is the modern version with arrays, better string handling, and more. For scripts on your own machine, always use #!/bin/bash. Only use #!/bin/sh for maximum portability — like scripts inside Docker containers where bash might not exist.cron — Linux's built-in scheduler, reliable since the 1970s. Run crontab -e to open the editor, add a line like 0 2 * * * /home/you/myscript.sh to run daily at 2am. The five fields are: minute, hour, day-of-month, month, day-of-week. Confused? Use crontab.guru — free visual cron builder../script.sh hello world. Inside the script, $1 is "hello", $2 is "world", and so on. $0 is the script name. $# is the count of arguments passed. Instead of hardcoding a folder path, accept it as $1 and the same script becomes reusable anywhere.#!/bin/bash as the very first line. (2) Wrong path — verify with ls /your/path. (3) Windows line endings if you edited on Windows — fix with sed -i 's/\r//' yourscript.sh. (4) Add set -x right after the shebang — it prints every command before it runs so you see exactly where it breaks.set -e near the top of your script. It tells bash to exit the moment any command fails. Add set -u to catch undefined variables, and set -o pipefail to catch errors inside pipes. All three together: set -euo pipefail. This is the single best habit for writing reliable scripts.// about this site
I've been using Linux for a while now, and I still find myself Googling the same bash commands constantly. Not because I forget them — but because I can never remember the exact syntax for the edge case I'm dealing with right now. The find command with -mtime? The awk trick to grab column five? Every. Single. Time.
Most resources out there are either too basic ("here is what echo does") or way too deep ("here is a 47-step tutorial"). There's a gap in the middle — a place for real, working scripts that handle actual everyday tasks, written in a way that someone who isn't a shell wizard can understand and modify.
So I built this. Every snippet here is something I've used in a real situation. Explanations are written how I wish someone had explained it to me. Nothing padded out for SEO. If a script is short, the explanation is short. If something has a gotcha, I call it out.
This site is updated regularly. If there's something you keep searching for and can't find here, send a note — I'll add it. For the full story, visit the About page.