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 below runs on Ubuntu, Debian, Fedora, and macOS. I've personally tested each one. The comment above each block tells you exactly what it does before you run it.
#!/bin/bash # Copies a folder to /backup with today's timestamp. # Run it 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. Backup saved to: backup_$DATE"
#!/bin/bash # Renames every .txt file in the current folder to .md # Useful after exporting notes, docs, or data dumps. for file in *.txt; do # Skip if no .txt files found [ -f "$file" ] || continue newname="${file%.txt}.md" mv "$file" "$newname" echo "Renamed: $file → $newname" done
#!/bin/bash # Checks disk usage and prints a warning if over 80%. # Great to run daily via cron on a server. THRESHOLD=80 USAGE=$(df / | awk 'NR==2{print $5}' | tr -d '%') if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "⚠ Heads up: disk is at ${USAGE}% — time to clean up" else echo "✓ Disk looks fine: ${USAGE}% used" fi
#!/bin/bash # Deletes .log files older than 30 days from a folder. # Run the echo version first to preview — then switch to -delete. LOG_DIR="/var/log/myapp" DAYS=30 # Safe preview mode (remove 'echo' to actually delete): find "$LOG_DIR" -type f -name "*.log" \ -mtime +$DAYS -delete echo "Cleaned logs older than $DAYS days from $LOG_DIR"
#!/bin/bash # Prints the key stats I always want to see at a glance. # Alias this to something like 'syscheck' 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 inside ~/work # I use this as my first command every morning. BASE="$HOME/work" TODAY=$(date +%Y-%m-%d) DIR="$BASE/$TODAY" mkdir -p "$DIR" echo "Ready. Working in: $DIR" # Optional: open it in your file manager # xdg-open "$DIR"
// interactive tool
Not sure where to start? Pick what you want to do and this'll generate a working starter script. No coding knowledge needed — just change the paths to match your setup.
⚡ Build Your Script
Choose a task below and the script generates automatically. Copy it, paste it into a .sh file, make it executable with chmod +x yourscript.sh, and run it.
// coming up
I'm adding new snippets regularly. These are the topics in the queue — each one will get its own set of examples with explanations.
// common questions
These are the questions I see over and over in forums, Discord servers, and comment sections. Answered as plainly as I can manage.
.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. That's it. If you skip the chmod step, you'll get a "Permission denied" error — that's the most common beginner trip-up. Alternatively, skip chmod entirely and just type bash yourscript.sh — works the same way.
bash --version to confirm. On Windows, install Git Bash (free, takes about 2 minutes) or enable WSL from Settings. Both give you a full bash environment with zero cost.
sh is the old, minimal shell — it works everywhere but has fewer features. bash is the upgraded version with arrays, better string manipulation, and more modern syntax. For anything you're writing for your own machine, always use #!/bin/bash. Only use #!/bin/sh if you're writing a script for very minimal environments like Docker containers or embedded systems where bash might not be installed.
cron — it's Linux's built-in scheduler and it's been reliable since the 1970s. Open the cron editor with crontab -e, then add a line like 0 2 * * * /home/you/myscript.sh to run every day at 2am. The five numbers mean: minute, hour, day-of-month, month, day-of-week. If that format confuses you, use crontab.guru — it's a free visual cron builder that's genuinely useful.
./script.sh hello world. Inside your script, $1 gets "hello", $2 gets "world", and so on. $0 is the script's own filename. $# tells you how many arguments were passed. This is how you make scripts flexible — instead of hardcoding a folder path, pass it in as $1 and the same script works anywhere.
#!/bin/bash as the very first line — add it. (2) The path to your file or folder is wrong — double-check with ls /your/path first. (3) You have Windows line endings in your file (CRLF instead of LF) — this happens when editing on Windows. Fix it with: sed -i 's/\r//' yourscript.sh. (4) Add set -x right after the shebang line to enable debug mode — it prints every command before it runs, which usually reveals the problem immediately.
// 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 of the 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 a person who isn't a shell scripting wizard can understand and modify.
So I built this. Every snippet on this page is something I've used in a real situation. The explanations are written the way I wish someone had explained it to me. Nothing is 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 a snippet you keep searching for and can't find here, send me a note — I'll add it.