Premium
BASH Snippets.

Curated list of production-ready BASH scripts and coding solutions.

BASH

Sync Local Directory to Remote Server with rsync

Automate efficient and incremental file synchronization from your local machine to a remote web server using rsync over SSH, perfect for deployments.

View Snippet →
BASH

Filter and Count HTTP Error Codes from Nginx Access Logs

Quickly analyze Nginx access logs to identify and count specific HTTP error codes (e.g., 4xx, 5xx) using `grep`, `awk`, and `sort` for web server monitoring.

View Snippet →
BASH

Automated MySQL Database Backup with Timestamp

Create a robust bash script to automate daily MySQL database backups, compressing them with a timestamp for easy recovery and storage management.

View Snippet →
BASH

Monitor and Restart a Systemd Service if Down

Create a bash script to periodically check the status of a critical systemd service (e.g., Nginx, PHP-FPM) and automatically restart it if it's found to be inactive.

View Snippet →
BASH

Parse Command Line Arguments with getopts for Script Customization

Learn to build robust bash scripts that accept and parse command-line arguments using `getopts`, enabling flexible execution with options and values.

View Snippet →
BASH

Extracting and Analyzing Web Server Logs

Learn to process web server access logs using `grep` and `awk` to extract common errors, unique IP addresses, and request statistics for debugging.

View Snippet →
BASH

Check and Restart a Linux Service by Name

Automate checking if a critical Linux service (e.g., Nginx, a Node.js process) is running and restart it if it's found to be inactive, ensuring application uptime.

View Snippet →
BASH

Define Global Environment Variables and Aliases

Create a Bash script to set up common environment variables and useful command aliases for all new shell sessions, enhancing developer productivity and consistency.

View Snippet →
BASH

Robust Error Handling and Logging for Bash Scripts

Learn to implement robust error handling, trap signals, and redirect all script output to a log file, ensuring critical operations are reliably monitored and debugged.

View Snippet →
BASH

Parsing Command Line Arguments in Bash with `getopts`

Learn to efficiently parse short command-line options and their arguments using `getopts` in Bash, making your scripts more flexible and user-friendly.

View Snippet →
BASH

Check Open Port and Identify Process in Bash

Discover how to use Bash to check if a specific network port is in use and identify the process (PID and name) currently listening on it, useful for debugging port conflicts.

View Snippet →
BASH

Monitor Disk Usage and Send Email Alerts in Bash

Implement a Bash script to monitor disk space usage on your server and send an email alert when a specified threshold is exceeded, preventing critical storage issues.

View Snippet →