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.

62 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 13 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 is ShellCheck-clean and explains the failure cases, not just the happy path.

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

A deleted file on Linux has no undo. This bash backup script timestamps every cp -r run with a date string, so no backup ever overwrites the one before it.

backup · cron-ready · rsync

Full guide →
intermediate · cleanup

Delete Old Log Files

Unmanaged logs fill /var/log until writes fail and services crash. find -mtime deletes .log files older than N days; preview with -print before deleting.

cleanup · find · cron-ready

Full guide →
beginner · monitor

Quick System Info Report

Guessing server state mid-outage costs time. One bash script reports hostname, uptime, CPU load, RAM, disk usage and IP address in a single run, no packages.

monitor · reporting · system

Full guide →
beginner · grep

Search Files for Text with grep

Opening files one by one to find a string wastes time. grep -rn searches every file under a directory and prints each match with its filename and line number.

grep · search · text

Full guide →
beginner · monitor

Check If Website Is Up

Learning a site is down from a user complaint means the outage already cost you. A bash curl check that alerts on any non-200 status, ready for a 5-minute cron.

monitor · curl · uptime

Full guide →
intermediate · error-handling

Bash Error Handling with set -euo pipefail

Bash keeps going after a failed command, so a broken cd followed by rm -rf hits the wrong directory. set -euo pipefail stops the script at the first failure.

error-handling · best-practices · set

Full guide →
beginner · conditionals

Bash If/Else Examples

The wrong comparison operator makes a bash if statement fail silently on unexpected input. if/else, elif, integer and string tests, file checks, and quoting.

conditionals · basics · if

Full guide →
beginner · files

Create a Dated Folder

Backup folders without dates overwrite old runs and sort unpredictably. date +%Y-%m-%d names each folder so ls sorts them chronologically, no packages needed.

files · date · mkdir

Full guide →

Free Browser Tools for Bash

No install required. Open in your browser, get the answer.

The cron builder generates expressions visually and shows the human-readable schedule. The chmod calculator outputs octal, symbolic, and the exact chmod command. The exit code lookup explains what any exit code 0–255 means and which command typically produces it. All 13 tools run entirely in your browser — no server round-trips, no data collected.

See all 13 tools →

ShellCheck deep dives: SC2086 · SC2046 · SC2063 · SC2115 · SC2154 · SC2034 · SC2016

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

Bash Error Messages, Decoded

Nine errors, each reproduced: what the shell was doing, the one check that names the cause, and the fix. command not found to Connection refused.

guide

systemd Timers vs Cron

The run cron skipped because the laptop was off, the tick a slow job queues instead of overlapping, and the OnFailure= alert — measured side by side on one machine.

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 30-function shared library + a 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →