Command Line Cheatsheet

Useful commands for Bash (Bourne-again shell), the default shell for most Linux distributions and macOS. Most of these commands will also work with more advanced shells like ZSH (Z shell).

I don’t remember anything!

Use the history command to list all the commands you previously typed:

$> history
24 ls
25 cd foo
26 ssh jde@archidep.ch
27 cat file.txt

Note that each command is prefixed by a number. Type an exclamation mark followed by that number to retrieve the command:

$> !26
$> ssh jde@archidep.ch

If you’re looking for a specific command, you can filter your command history by piping it into the grep command. For example, this will search for all commands containing the word “ssh” in your history:

$> history | grep ssh
19 ssh-keygen --help
20 ssh-keygen
26 ssh jde@archidep.ch

Stop a running command (Ctrl-C)

Some commands take a long time, and some never stop by themselves. While one is running, you have no prompt. Press Ctrl-C (hold the Control key and press C) to interrupt it and get your prompt back:

$> sleep 3600
^C
$>

On macOS too, it is the Control key, not Command. Ctrl-C forces the command to stop: it might not have finished what it was doing.

Getting help (man, --help)

  • man ls displays the manual of the ls command on Unix systems (including the WSL).
  • git --help displays the help of the git command. Many (but not all) commands provide a help page.

Where am I? (pwd)

Use the pwd command, meaning print working directory:

$> pwd
/Users/jde/Downloads

What is there? (ls)

Use the ls command, meaning list:

Command Effect
ls List the files in the current directory (invisible files are hidden)
ls -a List all files in the current directory, including invisible ones
ls -ahl List all files in the current directory, also displaying their mode, owner, group, size and last modification date
ls foo List all files in the foo directory inside the current directory

Move around (cd)

Use the cd command, meaning change directory:

Command Effect
cd . Move into the current directory (wheeeeee)
cd foo Move into the foo directory inside the current directory
cd ./foo Same as previous
cd foo/bar Move into the bar directory inside the foo directory inside the current directory
cd ./foo/bar Same as previous
cd .. Move into the parent directory (e.g. into /foo if you are in /foo/bar)
cd ../.. Move into the parent directory of the parent directory (e.g. into / if you are in /foo/bar)
cd ~ Move into your home directory
cd Same as previous
cd / Move to the root of the file system

Special paths (., .., ~)

Path Where
. The current directory (the same as indicated by pwd)
.. The parent directory (e.g. /foo if you are in /foo/bar)
~ Your user’s home directory (e.g. /Users/username in macOS)
/ The file system’s root directory on Unix systems

Reading

What’s in this file? (cat, head, tail, less)

Display the entire contents of a file in the CLI with the cat command (as in concatenate):

$> cat file.txt
Hello
World

Display the first or last N lines (10 by default) of a file with the head and tail commands, respectively:

$> head file.txt
$> head -n 100 file.txt
$> tail file.txt
$> tail -n 50 file.txt

Display a large file in interactive mode, allowing you to scroll with the up and down arrow keys (exit this mode by typing the letter q, as in quit):

$> less file.txt

In less, type / followed by a word and press Enter to search for that word. Press n to go to the next match.

Writing

Create a directory (mkdir)

mkdir stands for make directory.

Create one directory in the current directory:

$> mkdir foo

Create a directory and all missing intermediate directories:

$> mkdir -p foo/bar/baz/qux

Create a file (echo, touch)

Create an empty file:

$> touch file.txt

Create (or overwrite) a file containing one line:

$> echo "Hello World!" > file.txt
💎Tip

Do the same with the tee command if you need to create a file with administrative privileges:

$> echo "Hello World!" | sudo tee file.txt

Append one line at the end of a file (also creates the file if it does not exist):

$> echo "Hello World!" >> file.txt
💎Tip

Again with the tee command if you need to create a file with administrative privileges:

$> echo "Hello World!" | sudo tee -a file.txt

Edit a file (nano)

Edit a file with nano:

$> nano /path/to/file.txt
💎Tip

Nano will always display available commands at the bottom of the screen. Ctrl-X is the one you will use most often, which saves and exits. Note that nano will ask you to confirm the file name. Just press Enter (unless you want to save your changes to another file, to keep the original one).

If you need superuser privileges to access the file, you can use sudo:

$> sudo nano /path/to/file.txt

Copy stuff (cp)

Copy a file to another location:

$> cp oldname.txt newname.txt
$> cp foo/oldname.txt bar/baz/newname.txt

Recursively copy a directory and all its contents:

cp -R olddirectory newdirectory

Move stuff (mv)

Move a file (or directory):

$> mv file.txt /somewhere/else
$> mv directory /somewhere/else

Rename a file (or directory), by moving it to a new name:

$> mv oldname.txt newname.txt

Delete stuff (rm)

Delete (or remove) a file:

$> rm file.txt

You can also delete a directory with rm by adding the -r (recursive) option.

$> rm -r directory

Be EXTREMELY careful when you type this command. One wrong move and you could permanently lose a lot of files (e.g. if you misspell the name of the directory you want to delete).

Finding stuff

Find files (find)

Recursively list all files in the current directory:

$> find .

Recursively find a file by its exact name, however deep it is buried. find prints the path of each file it found, which you can then use with another command:

$> find . -name notes.txt
./projects/archidep/notes.txt

Recursively list all JavaScript files in the current directory:

$> find . -name "*.js"

Find lines in files (grep)

Recursively find all files in the current directory containing the word “foo”:

$> grep -R foo .

Running programs

Run a program (./program)

When you are in the directory that contains a program, run it by writing ./ before its name:

$> ./program

The ./ is not decoration: it is a path to the program, since . is the current directory. Without it, the shell only looks for a command named program in the directories of your $PATH, and answers command not found.

Any other path to the program works too, relative or absolute, from wherever you are:

$> tools/program
$> ../tools/program
$> ~/tools/program
$> /home/jde/tools/program

Make a file executable (chmod +x)

A file is only a program if it has the permission to be executed. A script you just wrote or downloaded often does not have it yet, and the shell refuses to run it:

$> ./program
permission denied: ./program

Add that permission with the chmod command (change mode), +x meaning add the execute permission:

$> chmod +x program
$> ./program

Running a program gives it the right to do anything you can do on your computer: read, change or delete any of your files. Only make a file executable if you trust where it comes from.

Environment variables

Display an environment variable:

$> echo $FOO
$> echo $PATH

Set an environment variable (it will be gone when you close the CLI):

$> export FOO=bar
$> echo $FOO

To be able to keep this environment variable in all future CLIs, save the export FOO=bar line to your shell configuration file: ~/.bashrc for Bash (e.g. in the WSL or on Linux), or ~/.zshrc for Zsh (the default on macOS).

$PATH

$PATH is an environment variable that contains a colon-delimited (:) list of directories. When you type a command such as git, Bash will look for a binary file named git in each of these directories one by one and execute the first one it finds.

If it doesn’t find such an executable, it will return a command not found error.

$> echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

You can modify your path by adding export statements to your shell configuration file (~/.bashrc or ~/.zshrc):

# Add the /foo directory to the beginning of $PATH
export PATH="/foo:$PATH"
# Add the /opt/local/bin directory to the beginning of $PATH
export PATH="/opt/local/bin:$PATH"

Do I have that command? (which)

Locate an executable in the $PATH with the which command:

$> which git
/usr/local/bin/git
$> which foo
foo not found

Miscellaneous

Can I write a long command on multiple lines?

$> echo \
"Hello" \
"World"
Hello World