Do the same with the tee command if you need to create a file with
administrative privileges:
$> echo "Hello World!" | sudo tee file.txt
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).
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
man, --help)man ls displays the manual of the ls command on Unix systems.help ls displays the help of the ls command in Git Bash on Windows.git --help displays the help of the git command. Many (but not all)
commands provide a help page.pwd)Use the pwd command, meaning print working directory:
$> pwd
/Users/jde/Downloads
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 |
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 |
., .., ~)| 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 |
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
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
echo, touch)Create an empty file:
$> touch file.txt
Create (or overwrite) a file containing one line:
$> echo "Hello World!" > file.txt
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
Again with the tee command if you need to create a file with administrative
privileges:
$> echo "Hello World!" | sudo tee -a file.txt
nano)Edit a file with nano:
$> nano /path/to/file.txt
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
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
mv)Move a file (or directory):
$> mv file.txt /somewhere/else
$> mv directory /somewhere/else
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).
find)Recursively list all files in the current directory:
$> find .
Recursively list all JavaScript files in the current directory:
$> find . -name "*.js"
grep)Recursively find all files in the current directory containing the word “foo”:
$> grep -R foo .
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 ~/.bash_profile file (create it if it doesn’t
exist).
$PATH$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:/sbin
You 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"
which)Locate an executable in the $PATH with the which command:
$> which git
/usr/local/bin/git
$> which foo
foo not found
$> echo \
"Hello" \
"World"
Hello World