Skip to content
$bash-snippets

Bash Script Library

30 copy-paste bash scripts with plain-English explanations. Tested on Ubuntu 22.04 LTS and macOS Ventura.

30scripts
10free tools
0logins required

Beginner Scripts (12)

beginner · monitor · cron-ready · df

Disk Space Warning Script

Copy a disk space warning bash script using df, thresholds, cron, and email alerts. Monitor Linux se...

Full guide →
beginner · backup · cron-ready · rsync

Automated File Backup

Accidental deletion or disk failure permanently destroys data with no undo on Linux. Timestamps each...

Full guide →
beginner · monitor · reporting · system

Quick System Info Report

Guessing server state during an outage costs response time. One bash script snapshots hostname, upti...

Full guide →
beginner · grep · search · text

Search Files for Text with grep

Opening files manually to find a pattern across a codebase wastes time. grep -rn searches every file...

Full guide →
beginner · monitor · curl · uptime

Check If Website Is Up

Discovering a site is down from a user complaint means hours of lost traffic already gone. curl -s a...

Full guide →
beginner · conditionals · basics · if

Bash If/Else Examples

Comparison operator mistakes in bash scripts cause silent logic failures on unexpected input. Covers...

Full guide →
beginner · files · date · mkdir

Create a Dated Folder

Backup directories without timestamps overwrite previous runs and sort unpredictably. date +%Y-%m-%d...

Full guide →
beginner · disk · du · find · cleanup · troubleshooting

Find Large Files in Linux

Your disk hit 100% and the server stopped. Find the biggest files and directories fast with du and f...

Full guide →
beginner · ports · lsof · ss · kill · networking · troubleshooting

Kill Process on Port

EADDRINUSE means something is squatting on your port. Find the process with lsof or ss, then kill it...

Full guide →
beginner · ports · networking · ss · netstat · security · audit · lsof

List All Open Ports on Linux

A bash script that maps every port your server is listening on, along with the process name holding...

Full guide →
beginner · docker · containers · disk · cleanup · devops · pruning

Docker Cleanup Bash Script — Reclaim Disk Space from Docker Garbage

A bash script that removes stopped containers, unused images, dangling volumes, and build cache from...

Full guide →
beginner · bash · loops · for · scripting · beginner

Bash For Loop Examples

A for loop over the output of ls word-splits on filenames with spaces and silently skips files. Loop...

Full guide →

Intermediate Scripts (18)

intermediate · cleanup · find · cron-ready

Delete Old Log Files

Unmanaged log files silently fill /var/log until disk writes fail and services crash. find -mtime de...

Full guide →
intermediate · error-handling · best-practices · set

Bash Error Handling with set -euo pipefail

Bash silently continues after failed commands by default — a broken cd followed by rm -rf destroys t...

Full guide →
intermediate · process · pkill · pgrep

Kill a Process with pkill and pgrep

The ps/grep/copy-PID/kill workflow takes four steps every time you need to stop a process. pkill by...

Full guide →
intermediate · chmod · security · find

File Permissions Security Audit

World-writable files on a web server let any compromised script overwrite your application. find -pe...

Full guide →
intermediate · monitor · system · awk

Monitor CPU and RAM Usage

A runaway process consuming 100% CPU goes undetected until the server becomes unresponsive. top -bn1...

Full guide →
intermediate · email · alert · mailx

Send Email Alerts from Bash

Monitoring scripts without email alerts mean failures go unnoticed until users report them. Wraps ma...

Full guide →
intermediate · mysql · backup · cron-ready

MySQL Database Backup Script

A mistaken DROP TABLE or storage failure permanently destroys database data with no built-in undo. m...

Full guide →
intermediate · ssh · security · keys

SSH Key Setup Script

Password-based SSH is vulnerable to brute-force attacks and credential leaks on any internet-exposed...

Full guide →
intermediate · files · disk · find · awk

Find Duplicate Files in Linux

Duplicate files accumulate silently in archives and download folders, wasting gigabytes of disk spac...

Full guide →
intermediate · systemd · monitor · cron-ready

Restart a Service If It Stopped

A crashed nginx or postgresql stays down for hours without a watchdog to detect and recover it. syst...

Full guide →
intermediate · rsync · backup · ssh · cron · offsite · devops

Rsync Remote Backup

A local-only backup dies with the machine. Push an incremental, resumable copy to a remote server wi...

Full guide →
intermediate · ssl · openssl · security · monitoring · cron · networking

Check SSL Certificate Expiry with Bash

A bash script that connects to any domain over TLS, reads the certificate, and tells you how many da...

Full guide →
intermediate · bash · read · loops · files · intermediate

Read a File Line by Line in Bash

A while-read loop silently dropped the last server in a monitoring list because the file had no trai...

Full guide →
intermediate · bash · functions · arguments · scripting · intermediate

Bash Functions and Arguments

A function reused the variable name 'target' without declaring it local, overwrote the caller's vari...

Full guide →
intermediate · bash · functions · scripting · local-scope

Bash Functions: Return Values, Local Scope, and Reusable Logic

A bash function cannot return a string with return — that keyword sets an exit code only. Use echo p...

Full guide →
intermediate · bash · arrays · associative-arrays · scripting

Bash Arrays: Indexed, Associative, Append, and Safe Iteration

Storing a list as a space-separated string breaks the moment one element contains a space, splitting...

Full guide →
intermediate · bash · arguments · getopts · cli

Bash Argument Parsing: Positional Args, getopts, and Long Flags

A script that reads $1 as a value will accept --env as that value and deploy nowhere, silently. Pars...

Full guide →
intermediate · bash · strings · parameter-expansion · scripting

Bash String Manipulation: Substrings, Replace, and Parameter Expansion

Field-counting with cut -d/ -f3 returns the wrong slice the moment a URL gains an s for https. Param...

Full guide →

Free Interactive Tools

Need a cron expression? chmod calculator?

10 browser-based bash tools — no install required

Browse All Tools →
PAID RESOURCE — $9

The Production Bash Toolkit

6 scripts + shared library + 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →