Bash Cheat Sheet
A searchable reference for common bash and shell commands, filterable by category.
| Snippet | What it does |
|---|---|
pwd | Print the full path of the current working directory. |
ls | List the files and directories in the current location. |
ls -la | List all entries, including hidden ones, in long format with permissions and sizes. |
cd dir | Change into the directory named dir. |
cd .. | Move up one level to the parent directory. |
cd - | Switch back to the previous directory you were in. |
tree | Show the directory structure below the current folder as an indented tree. |
cp a b | Copy file a to path b. |
cp -r src dst | Copy a directory and everything inside it recursively. |
mv a b | Move or rename a to b. |
rm file | Delete a file permanently, with no recycle bin. |
rm -rf dir | Delete a directory and all of its contents without prompting for confirmation. |
mkdir -p a/b/c | Create a directory path, making any missing parent directories along the way. |
touch file | Create an empty file, or update the modified time of an existing one. |
ln -s target link | Create a symbolic link named link that points at target. |
cat file | Print the entire contents of a file to the terminal. |
less file | View a file one screen at a time, with scrolling and search. |
head file | Show the first ten lines of a file. |
tail -f file | Print the last lines of a file and keep streaming new lines as they are added. |
find . -name "*.js" | Search the current tree for files whose name matches a pattern. |
cmd1 | cmd2 | Pipe the output of cmd1 into cmd2 as its input. |
cmd > file | Redirect standard output to a file, replacing any existing contents. |
cmd >> file | Redirect standard output to a file, appending to the end. |
cmd 2>&1 | Send standard error to the same place as standard output. |
cmd < file | Feed the contents of a file into the command as standard input. |
cmd | tee file | Show output on screen and write a copy to a file at the same time. |
cmd > /dev/null | Discard output by sending it to the null device. |
cmd | xargs rm | Build and run a command from the items read on standard input. |
VAR=value | Assign a value to a variable, with no spaces around the equals sign. |
export VAR=value | Set a variable and export it so child processes can see it. |
$VAR | Expand to the value stored in the variable VAR. |
${VAR} | Expand VAR using explicit braces, useful when text follows the name. |
$(cmd) | Command substitution: replaced by the output of the command inside. |
${VAR:-default} | Use the value of VAR, or default when VAR is unset or empty. |
$1 | A positional parameter holding the first argument to a script or function. |
$@ | All positional arguments, expanded as separate quoted words when in double quotes. |
$# | The number of positional arguments that were passed. |
arr=(a b c) | Create an indexed array with three elements. |
${arr[@]} | Expand to every element of the array arr. |
if ...; then ...; fi | Run the commands after then only when the test condition succeeds. |
[ -f path ] | Test whether path exists and is a regular file. |
[ -z "$s" ] | Test whether the string is empty (zero length). |
[ -n "$s" ] | Test whether the string is not empty. |
[ "$a" == "$b" ] | Test whether two strings are equal. |
[ "$a" -eq "$b" ] | Test whether two integers are equal. |
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds (exit status zero). |
cmd1 || cmd2 | Run cmd2 only if cmd1 fails (nonzero exit status). |
case ... esac | Branch on a value by matching it against several patterns. |
for x in a b c; do ...; done | Loop over a list of words, running the body once for each. |
while ...; do ...; done | Repeat the body for as long as the condition stays true. |
until ...; do ...; done | Repeat the body until the condition becomes true. |
name() { ...; } | Define a reusable shell function named name. |
break | Exit the innermost loop immediately. |
continue | Skip the rest of this iteration and start the next one. |
grep pattern file | Print the lines of a file that match a pattern. |
grep -r pattern dir | Search a whole directory tree recursively for a pattern. |
sed 's/a/b/g' file | Stream edit text, here replacing every a with b on each line. |
awk '{print $1}' file | Process text by fields, here printing the first whitespace field of each line. |
cut -d, -f1 file | Cut out a chosen field, here the first comma-separated column. |
sort file | Sort the lines of a file into order. |
sort file | uniq | Collapse adjacent duplicate lines, which requires sorted input first. |
wc -l file | Count the number of lines in a file. |
tr 'a-z' 'A-Z' | Translate characters, here turning lowercase letters into uppercase. |
ps aux | List every running process with its owner and resource usage. |
kill PID | Send the default terminate signal to the process with that id. |
kill -9 PID | Force kill a process that will not stop with the normal signal. |
jobs | List the background jobs started from the current shell. |
bg %1 | Resume a stopped job in the background. |
fg %1 | Bring a background job to the foreground. |
nohup cmd & | Run a command so it keeps going after you close the terminal. |
cmd & | Run a command in the background and get the prompt back right away. |
top | Show a live, updating view of processes and system load. |
chmod 644 file | Set file permissions using octal mode bits. |
chown user:group file | Change the owning user and group of a file. |
No snippets match your search.
Runs entirely in your browser. Nothing you type is sent anywhere; open DevTools and watch the Network tab to verify zero requests.
What this tool does
This is a searchable cheat sheet for everyday bash and shell usage, covering the commands and syntax you reach for in a terminal. Each row pairs a short snippet with a plain-English description of what it does, grouped into categories like navigation, files, redirection, variables, tests, loops, text processing, and processes. Type in the filter box to search by command or by what you are trying to do, or tap a category to narrow the list. The whole reference is baked into the page, so it works offline and sends nothing anywhere.
How to use it
Start typing in the Filter box. Entering grep surfaces
the text-search commands; entering redirect or a symbol like
>> narrows to the redirection rows; entering loop shows
the loop and function forms. The category buttons (Navigate, Files,
Redirection, Variables, Tests, Loops, Text, Processes) combine with the text filter, so
you can pick Text and type sort to zero in on sorting. Clear
the box to see everything again. Nothing is submitted and there is no result limit.
Common use cases
- Looking up the flag or syntax for a command you do not use every day.
- Writing a quick shell script and checking the shape of a for loop or an if test.
- Building a one-liner by chaining grep, sort, and uniq through pipes.
- Teaching or learning the basics of the command line with copyable examples.
- Double-checking a risky command like rm -rf before you run it.
Common pitfalls
- Unquoted variables get split. Writing
rm $filebreaks when the value contains spaces, because the shell splits it into several arguments. Quote your expansions as"$file"(and"$@") unless you specifically want that word splitting to happen. - rm -rf is unforgiving. There is no recycle bin; the files are gone
immediately. Double-check the path, avoid variables that might be empty (an empty
value in
rm -rf "$DIR"/can target the wrong place), and considerrm -iwhile you test. - Spaces around = matter. In an assignment you must write
VAR=valuewith no spaces. Adding spaces, as inVAR = value, makes the shell try to run a command called VAR instead. Inside a test, by contrast, you do need the spaces, as in[ "$a" = "$b" ]. - sh is not bash. Features like
[[ ... ]], arrays, and arithmetic$(( ... ))are bash extensions. A script with a#!/bin/shshebang may run under a leaner shell where those fail, so match the shebang to the features you actually use.
Frequently asked questions
- What is the difference between > and >>?
- Both send standard output to a file, but they treat existing content differently. A single > truncates the file first, so it starts empty and only the new output remains. A double >> appends, adding the new output to the end and keeping whatever was already there. Reach for >> when you want a log file to grow over time.
- What does 2>&1 do?
- In the shell, stream 1 is standard output and stream 2 is standard error. Writing 2>&1 redirects standard error to wherever standard output is currently going, merging the two streams. Order matters: cmd > file 2>&1 sends both to the file, while cmd 2>&1 > file only redirects errors to the old destination because the > file part is applied afterward.
- What is the difference between single and double quotes in bash?
- Single quotes are literal: nothing inside them is expanded, so a dollar sign and a name stay as plain text. Double quotes are expanding: variables and command substitution are evaluated, while spaces and most special characters are still protected. As a rule, use double quotes when you want variables to expand and single quotes when you want the exact characters.
- What is $() and how is it different from backticks?
- The $() form is command substitution: the shell runs the command inside and replaces the whole thing with its output. It does the same job as the older backtick form but is easier to read and, importantly, can be nested without awkward escaping. Prefer $(cmd) in new scripts.
- How do I loop over files in bash?
- Use a for loop with a glob, such as for f in *.txt; do echo "$f"; done. The shell expands the pattern into the matching file names before the loop runs, and each name is placed in the variable f in turn. Always quote "$f" so names with spaces are handled as a single item rather than being split.
- What does set -euo pipefail do?
- It is a common safety header for bash scripts. set -e exits as soon as any command fails, set -u treats the use of an unset variable as an error, and set -o pipefail makes a pipeline fail if any stage fails rather than only the last one. Together they turn silent mistakes into loud, early failures.
- Does this cheat sheet run offline or send my commands anywhere?
- It runs entirely in your browser. The full command list is baked into the page at build time, and both the search box and the category filters work with local JavaScript. Nothing you type is uploaded, so you can open DevTools, watch the Network tab, and confirm there are zero requests while you use it.
Cite this tool
For academic, journalistic, or technical references. Pick a format:
Citations use 2026 as the publication year. Access date is left as a fillable placeholder where the citation style expects one.