Skip to content

$bashsnippets.xyz

Stop Googling the Same Bash Commands

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, with the edge cases documented so you don't hit the same problems I did.

45 Working Scripts

100% Tested on Linux

0 Logins Required

Free Always

What is BashSnippets?

BashSnippets is a free bash script library for Linux developers, sysadmins, and DevOps engineers. Every script solves a specific operational problem — disk space monitoring, automated backups, service watchdogs, log cleanup — and comes with a plain-English explanation of what each line does and why.

Bash runs on every Linux server without installing anything. That means these scripts work on Ubuntu, Debian, Fedora, CentOS, and macOS without a dependency manager, a runtime, or a package.json. Copy the script, set a threshold or path, make it executable, and you're done. The 12 browser tools — cron builder, chmod calculator, exit code lookup — work the same way: open in a browser, get the answer, no account required.

Copy-Paste Scripts That Work

Every snippet runs on Ubuntu 22.04 LTS, Debian 12, Fedora 39, and macOS Ventura. Tested personally — including the failure cases.

beginner · monitor

Disk Space Warning Script

Copy a disk space warning bash script using df, thresholds, cron, and email alerts. Monitor Linux servers and prevent full drives.

monitor · cron-ready · df

Full guide →
beginner · backup

Automated File Backup

Accidental deletion or disk failure permanently destroys data with no undo on Linux. Timestamps each cp -r run with a date string so backups never overwrite each other.

backup · cron-ready · rsync

Full guide →
intermediate · cleanup

Delete Old Log Files

Unmanaged log files silently fill /var/log until disk writes fail and services crash. find -mtime deletes .log files older than N days — preview with -print before removing from production.

cleanup · find · cron-ready

Full guide →
beginner · monitor

Quick System Info Report

Guessing server state during an outage costs response time. One bash script snapshots hostname, uptime, CPU load, RAM, disk usage, and IP address in one run — no extra packages.

monitor · reporting · system

Full guide →
beginner · grep

Search Files for Text with grep

Opening files manually to find a pattern across a codebase wastes time. grep -rn searches every file recursively and returns every match with filename and line number.

grep · search · text

Full guide →
beginner · monitor

Check If Website Is Up

Discovering a site is down from a user complaint means hours of lost traffic already gone. curl -s alerts on any non-200 HTTP status code — cron-schedulable for five-minute checks.

monitor · curl · uptime

Full guide →
intermediate · error-handling

Bash Error Handling with set -euo pipefail

Bash silently continues after failed commands by default — a broken cd followed by rm -rf destroys the wrong directory. set -euo pipefail exits on first failure before damage spreads.

error-handling · best-practices · set

Full guide →
beginner · conditionals

Bash If/Else Examples

Comparison operator mistakes in bash scripts cause silent logic failures on unexpected input. Covers if/else, elif, integer and string test operators, file condition checks, and quoting safety.

conditionals · basics · if

Full guide →
beginner · files

Create a Dated Folder

Backup directories without timestamps overwrite previous runs and sort unpredictably. date +%Y-%m-%d auto-names folders so ls sorts them chronologically — no packages needed.

files · date · mkdir

Full guide →

Long-Form Bash Guides

When a copy-paste script isn't enough — deep dives into the failure modes behind cron jobs, CI/CD pipelines, text processing, and server maintenance.

guide

25 Bash Scripts Every Linux Sysadmin Needs

Disk-full outages, silent SSL expiry, dead services, permission holes — the scripts that prevent each failure, organized by failure mode.

guide

Bash Scripts That Survive Cron: Locking, Timeouts, and Retries

Overlapping runs, hung jobs, and silent 2am failures — the flock, timeout, and retry patterns that stop them.

guide

Bash Text Processing: find, grep, sed, and awk

The four commands that answer "what broke and where" — patterns for logs and config files you can paste into a shell.

guide

Bash Scripting for CI/CD Pipelines

GitHub Actions steps, deploy scripts, and Docker entrypoints that fail loudly in CI instead of silently in production.

guide

The Safe Bash Script Template: What set -euo pipefail Actually Changes

Strict mode is three flags, not one incantation — where errexit is silently switched off, why the ERR trap needs set -E, and the assembled template.

guide

Diagnosing a Hung Process: The Commands to Run Before You Kill It

S answers signals, D cannot be killed at all — read wchan and the syscall without root, check the socket queues, and capture the evidence before kill -9 destroys it.

guide

List Open Ports on Linux

What is listening, who owns it, and is it reachable — ss filters, the -e trick that names the owner without root, /proc/net/tcp by hand, docker-proxy, and a CSV audit that alerts on new listeners.

guide

Auto-Restart a Stopped Service on Linux

Crashed, stopped on purpose, or running but not answering — three kinds of "down", and the systemd Restart=, reset-failed, and probe-based watchdog that handles each without a restart loop.

guide

Shell Scripts That Talk to APIs

curl that fails when the API fails, jq instead of regex, and a Slack alert when it breaks — the pattern for calling HTTP from bash without silent poison downstream.

See all guides →

Common Questions

faq — bash

What is a bash script?

A text file of terminal commands that runs automatically in sequence. Saved with a .sh extension, starting with #!/bin/bash.

faq — bash

How do I run a bash script?

Make it executable: chmod +x script.sh — then run: ./script.sh. Or skip chmod and type: bash script.sh

faq — bash

How do I schedule a bash script?

Use cron. Run crontab -e and add a line like: 0 2 * * * /path/to/script.sh to run daily at 2am.

faq — bash

How do I stop my script on errors?

Add set -euo pipefail near the top. This exits on error, catches undefined variables, and handles pipe failures.

PAID RESOURCE — $9

The Production Bash Toolkit

An operational script system + a 31-function shared library + a 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →