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.
Getting Help
- 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--help
option will often print out a short help message.man
<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
man
pages.
Stopping
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
man
,less
, andtop
) use the Q key to quit.
File Navigation
pwd
- Print the present working directory (ie. the current folder) you are in.cd
[path]
- Change directory to the given path. If no path is given, changes to your home directory.ls
[path]
- List the contents of the given path, or the current directory if none is given. Common options include-a
to show all files and-l
to use the long format (ie. more details).
File Manipulation
mkdir
<path>
- Make a directory (the given path). Use-p
to also create intermediate (prerequiste) directories as needed.mv
<file>
<path>
- Move a file to a directory.cp
<file>
<path>
- Copy a file to a directory.rm
<file>
- Remove a file. This does not ask for confirmation and cannot be undone.rmdir
<path>
- Remove a directory. Only works if the directory is already empty. This does not ask for confirmation and cannot be undone.less
<file>
- Display a file. Pressq
to quit.
Text Manipulation
nano
<file>
- Edit a file. The available commands are listed at the bottom, with the^
meaning CTRL (eg. CTRL+X to exit).head
<file>
- Print the first 10 lines of a file. Use-n 20
to print (for example) the first 20 lines.tail
<file>
- Print the last 10 lines of a file. Use-n 20
to print (for example) the last 20 lines.wc
<file>
- Show a word count of a file.sort
<file>
- Sort a file by lines. Use-i
to ignore case and-n
to treat the lines as numbers.grep
<text>
<file>
- Print the lines where<text>
exists in<file>
. Use-i
to ignore case and -r to recursively search through directories.uniq
- Print theuniq
ue lines of a file. Assumes the file to be sorted first. Use-d
to show duplicates instead and-c
to count each unique line.
System Status
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-h
to get human-readable sizes.du
<file>
- Show the amount of space taken up by the file. The current directory is used if no file is given. Use-h
to get human-readable sizes, and-s
to get separate sizes for each file.uname
- Print system information. Use-a
to show all information.
Other Utilities
cal
[[month] year]
- Print a calendar, optionally specifying year and month.diff
<file1>
<file2>
- Find the difference between two files. Use-y
to show the files side-by-side.
Advanced Topics
- 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 -c
will 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 searchfile2
for the first line offile1
. - Variables, Branches, Loops - Bash is a full programming language with variables, branches, and loops. Check the Advanced Bash Scripting Guide for more information.