Bash Cheatsheet
Navigate, search, and automate tasks in the terminal with the most useful Bash commands.
When Bash Is the Right Tool
Bash is a strong fit for small automation tasks, deployment glue code, log inspection, file operations, and command pipelines. It is less ideal when the logic becomes stateful, deeply nested, or difficult to test, which is usually the point to switch to a general-purpose language.
Navigation
| Key / Code | Description |
|---|---|
| pwd | Print working directory. |
| ls -la | List files with details. |
| cd ~/projects | Change directory. |
| cd - | Go back to previous directory. |
Files & Folders
| Key / Code | Description |
|---|---|
| touch file.txt | Create a file. |
| mkdir -p app/src | Create nested folders. |
| cp -r src dist | Copy recursively. |
| mv old new | Move or rename. |
| rm -rf temp | Remove files or folders. |
Search & Text
| Key / Code | Description |
|---|---|
| grep -R "TODO" . | Search recursively. |
| find . -name "*.ts" | Find files by name. |
| sed -n '1,20p' file | Print a line range. |
| awk '{print $1}' file | Extract columns. |
Pipes
Chain commands to transform data.
cat access.log | grep 500 | sort | uniq -c | sort -nrScripting Basics
Quick helpers for automation.
#!/usr/bin/env bash
set -euo pipefail
NAME=devref
echo "Hello $NAME"
for f in *.log; do
echo "Processing $f"
doneCommon Bash Mistakes
The most common Bash problems come from unquoted variables, assuming filenames never contain spaces, and ignoring non-zero exit codes in pipelines. Using strict mode, quoting variables, and testing scripts with representative input prevents most of these failures.
Related
Use Bash together with the core Linux commands you reach for in daily terminal work.
Edit streams and files inline when your Bash scripts need quick transformations.
Extract columns and reshape structured text directly from shell pipelines.
Schedule your Bash scripts safely in production and server environments.