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 / CodeDescription
pwdPrint working directory.
ls -laList files with details.
cd ~/projectsChange directory.
cd -Go back to previous directory.

Files & Folders

Key / CodeDescription
touch file.txtCreate a file.
mkdir -p app/srcCreate nested folders.
cp -r src distCopy recursively.
mv old newMove or rename.
rm -rf tempRemove files or folders.

Search & Text

Key / CodeDescription
grep -R "TODO" .Search recursively.
find . -name "*.ts"Find files by name.
sed -n '1,20p' filePrint a line range.
awk '{print $1}' fileExtract columns.

Pipes

Chain commands to transform data.

cat access.log | grep 500 | sort | uniq -c | sort -nr

Scripting 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"
done

Common 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

Knowledge is power.