Bash Command Line Cheat Sheet
Most commands looks like this:
ls -l code
- The first part (
ls) is the command. Different commands do different things.
- The second part (
-l) is an option. They change how the commands work.
- The third part (
code) is an argument. They specify what the command acts on.
Options are always optional. Some commands (and some options) have required arguments. In this cheat sheet,
<argument> means that the argument is required;
[argument] means that the argument is optional.
- https://google.com/ - Google is your friend. Googling a command or a task will often give you multiple ways of doing what you need.
<command> --help- Calling the command with the
--helpoption will often print out a short help message.
<command>- Many commands have a manual you can read. It's on the verbose side and is not always easy to understand, but it's built into every system.
- https://explainshell.com/ - If you see a command you want every option and argument explained, ExplainShell will match each part with its explanation from the
exit- Log out of the command line.
- ctrl+c - Most commands can be stopped by pressing CTRL+C. Note that it's still Control, not ⌘ or Option or Alt, on a Mac.
- q - Some commands (notably
top) use the Q key to quit.
pwd- Print the present working directory (ie. the current folder) you are in.
[path]- Change directory to the given path. If no path is given, changes to your home directory.
[path]- List the contents of the given path, or the current directory if none is given. Common options include
-ato show all files and
-lto use the long format (ie. more details).
<path>- Make a directory (the given path). Use
-pto also create intermediate (prerequiste) directories as needed.
<path>- Move a file to a directory.
<path>- Copy a file to a directory.
<file>- Remove a file. This does not ask for confirmation and cannot be undone.
<path>- Remove a directory. Only works if the directory is already empty. This does not ask for confirmation and cannot be undone.
<file>- Display a file. Press
<file>- Edit a file. The available commands are listed at the bottom, with the
^meaning CTRL (eg. CTRL+X to exit).
<file>- Print the first 10 lines of a file. Use
-n 20to print (for example) the first 20 lines.
<file>- Print the last 10 lines of a file. Use
-n 20to print (for example) the last 20 lines.
<file>- Show a word count of a file.
<file>- Sort a file by lines. Use
-ito ignore case and
-nto treat the lines as numbers.
<file>- Print the lines where
-ito ignore case and -r to recursively search through directories.
uniq- Print the
unique lines of a file. Assumes the file to be sorted first. Use
-dto show duplicates instead and
-cto count each unique line.
date- Print the current date.
uptime- Print how long the computer has been on.
top- Show the most active programs. Press q to quit.
df- Show the amount of space available on the hard disk. Use
-hto get human-readable sizes.
<file>- Show the amount of space taken up by the file. The current directory is used if no file is given. Use
-hto get human-readable sizes, and
-sto get separate sizes for each file.
uname- Print system information. Use
-ato show all information.
[[month] year]- Print a calendar, optionally specifying year and month.
<file2>- Find the difference between two files. Use
-yto show the files side-by-side.
- Pipes - Many commands can be chained together with the pipe (
|), so the output of one becomes the input of the other. For example,
head <file> | sort | uniq -cwill count the unique items in the first ten lines of a file.
- Command Substitution - In commands that have a text argument (eg.
grep), the text could be the output of another command. For example,
grep "$(head -n 1 <file1>)" <file2>will search
file2for the first line of
- Variables, Branches, Loops - Bash is a full programming language with variables, branches, and loops. Check the Advanced Bash Scripting Guide for more information.