Easy Shell Cookbook: Quick Scripts for Everyday Tasks
Overview:
A compact, practical guide focused on short, reusable shell scripts that solve common daily tasks across Unix-like systems. Emphasizes readability, portability, and safety for beginners and intermediate users.
Key sections
-
Getting started
- Shell basics (bash, sh, zsh)
- Script structure, shebang, permissions
- Best practices: quoting, error handling, set -euo pipefail
-
File and text handling
- Batch renaming, file backups, safe deletions
- Grep/sed/awk recipes for parsing logs and CSVs
- Simple text transformations and reports
-
System maintenance
- Disk usage summaries, log rotation helpers
- Automated backups and pruning old files
- Service checks and restart scripts
-
Networking and downloads
- Simple wget/curl download managers
- Querying APIs with jq, automating uploads
- Local network scans and port checks
-
Automation and scheduling
- Cron job examples and idempotent scripts
- Locking to prevent concurrent runs
- Using systemd timers as alternatives
-
Developer utilities
- Quick build/test runners, cleanup scripts
- Simple deploy helpers and git automation snippets
-
Cross-platform tips
- POSIX-compatible constructs vs. Bash-specific features
- Handling differences between macOS and Linux
-
Safety and testing
- Dry-run modes, verbose logging, exits codes
- Minimal test harnesses and examples
Example recipes (brief)
- Backup a directory with timestamp:
bash
#!/usr/bin/env bash set -euo pipefail src=“\(1</span><span class="token" style="color: rgb(163, 21, 21);">"</span><span> </span><span></span><span class="token assign-left" style="color: rgb(54, 172, 170);">dest</span><span class="token" style="color: rgb(57, 58, 52);">=</span><span class="token" style="color: rgb(163, 21, 21);">"/backup/</span><span class="token" style="color: rgb(54, 172, 170);">\)(basename ”\(src</span><span class="token" style="color: rgb(54, 172, 170);">"</span><span class="token" style="color: rgb(54, 172, 170);">)</span><span class="token" style="color: rgb(163, 21, 21);">-</span><span class="token" style="color: rgb(54, 172, 170);">\)(date +%Y%m%d-%H%M%S”).tar.gz” tar -czf “\(dest</span><span class="token" style="color: rgb(163, 21, 21);">"</span><span> -C </span><span class="token" style="color: rgb(163, 21, 21);">"</span><span class="token" style="color: rgb(54, 172, 170);">\)(dirname ”\(src</span><span class="token" style="color: rgb(54, 172, 170);">"</span><span class="token" style="color: rgb(54, 172, 170);">)</span><span class="token" style="color: rgb(163, 21, 21);">"</span><span> </span><span class="token" style="color: rgb(163, 21, 21);">"</span><span class="token" style="color: rgb(54, 172, 170);">\)(basename “\(src</span><span class="token" style="color: rgb(54, 172, 170);">"</span><span class="token" style="color: rgb(54, 172, 170);">)</span><span class="token" style="color: rgb(163, 21, 21);">"</span><span> </span><span></span><span class="token builtin" style="color: rgb(43, 145, 175);">echo</span><span> </span><span class="token" style="color: rgb(163, 21, 21);">"Saved: </span><span class="token" style="color: rgb(54, 172, 170);">\)dest”
- Remove files older than 30 days:
bash
find /tmp/myapp -type f -mtime +30 -print -delete
- Simple API request and parse JSON:
bash
curl -s ‘https://api.example.com/data’ | jq -r ’.items[] | “(.id): (.name)”’
Who it’s for
- Beginners who want practical, copy-pasteable scripts.
- Intermediate users seeking idiomatic, safe patterns.
- DevOps and sysadmins needing quick reference snippets.
Outcome
Readers gain a toolbox of concise, tested scripts to automate routine tasks, reduce manual work, and learn safer shell practices.
Leave a Reply