Useful commands for Bash (Bourne-again shell), the default shell for most Linux distributions and macOS.
- I don't remember anything!
- Getting help (
man,--help) - Navigating
- Reading
- Writing
- Finding stuff
- Environment variables
Use the history command to list all the commands you previously typed:
$> history
24 ls
25 cd foo
26 ssh jdoe@archidep.ch
27 cat file.txtNote that each command is prefixed by a number. Type an exclamation mark followed by that number to retrieve the command:
$> !26
$> ssh jdoe@archidep.chIf 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 jdoe@archidep.chman lsdisplays the manual of thelscommand on Unix systems.help lsdisplays the help of thelscommand in Git Bash on Windows.git --helpdisplays the help of thegitcommand. Many (but not all) commands provide a help page.
Use the pwd command, meaning print working directory:
$> pwd
/Users/jdoe/DownloadsUse 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 |
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 |
| 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 |
Display the entire contents of a file in the CLI with the cat command (as
in concatenate):
$> cat file.txt
Hello
WorldDisplay 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.txtDisplay 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.txtmkdir stands for make directory.
Create one directory in the current directory:
$> mkdir fooCreate a directory and all missing intermediate directories:
$> mkdir -p foo/bar/baz/quxCreate an empty file:
$> touch file.txtCreate (or overwrite) a file containing one line:
$> echo "Hello World!" > file.txtDo the same with the
teecommand 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.txtAgain with the
teecommand if you need to create a file with administrative privileges:$> echo "Hello World!" | sudo tee -a file.txt
Edit a file with nano:
$> nano /path/to/file.txtNano will always display available commands at the bottom of the screen.
Ctrl-Xis 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.txtCopy a file to another location:
$> cp oldname.txt newname.txt
$> cp foo/oldname.txt bar/baz/newname.txtRecursively copy a directory and all its contents:
cp -R olddirectory newdirectoryMove a file (or directory):
$> mv file.txt /somewhere/else
$> mv directory /somewhere/elseDelete (or remove) a file:
$> rm file.txtYou can also delete a directory with rm by adding the -r (recursive)
option.
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).
$> rm -r directoryRecursively list all files in the current directory:
$> find .Recursively list all JavaScript files in the current directory:
$> find . -name "*.js"Recursively find all files in the current directory containing the word "foo":
$> grep -R foo .Display an environment variable:
$> echo $FOO
$> echo $PATHSet an environment variable (it will be gone when you close the CLI):
$> export FOO=bar
$> echo $FOOTo be able to keep this environment variable in all future CLIs, save the
export FOO=bar line to your ~/.bash_profile file (create it if it doesn't
exist).
$PATH is an environment variable that contains a semicolon-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:/sbinYou can modify your path by adding export statements to your ~/.bash_profile
file (create it if it doesn't exist):
# 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"
Locate an executable in the $PATH with the which command:
$> which git
/usr/local/bin/git
$> which foo
foo not found