Do the same with the tee command if you need to create a file with
administrative privileges:
$> echo "Hello World!" | sudo tee file.txtUseful 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
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.
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.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
In less, type / followed by a word and press Enter to search for that
word. Press n to go to the next match.
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.txtAppend 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.txtnano)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
Rename a file (or directory), by moving it to a new name:
$> mv oldname.txt newname.txt
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 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"
grep)Recursively find all files in the current directory containing the word “foo”:
$> grep -R foo .
The pipe (|) sends what a command prints into the next command, instead of to
your terminal. Most text commands read the lines you send them when you do not
name a file, so you can chain them: each one does one small job on those lines,
and passes the result on. The Unix Processes subject explains how
this works.
A pipe sends text, not arguments. echo file.txt | cat prints file.txt,
not the contents of the file: cat reads the text it receives as if it were the
contents of a file.
grep)Keep only the lines that contain a word. This is the most common use of a pipe: make a long output short enough to read.
$> ps -ef | grep nginx
$> ls /etc | grep ssh
$> apt list --installed | grep postgresql
| Option | Effect |
|---|---|
grep -i foo |
Ignore case: also keep lines with Foo or FOO |
grep -v foo |
Invert: keep only the lines that do not contain foo |
wc -l)Count the lines a command prints:
$> ls ~/guessit-ex/node_modules | wc -l
$> cat /etc/passwd | wc -l
$> grep -r TODO . | wc -l
wc -w counts words instead of lines.
head, tail)Keep the first or last lines of what a command prints (10 by default):
$> ls -t ~/Downloads | head -n 5
$> sudo journalctl -u ssh | tail -n 20
The first command shows the five files of your Downloads directory that were
modified last (ls -t sorts them by modification time, newest first).
cut)Keep one or more columns of each line. -d gives the character that separates
the columns (the delimiter), and -f the numbers of the columns to keep
(the fields):
$> cat /etc/passwd | cut -d : -f 1
root
daemon
...
$> cat /etc/passwd | cut -d : -f 1,7
root:/bin/bash
daemon:/usr/sbin/nologin
...
cut uses exactly one character as the delimiter. When the columns are
separated by several spaces, as in the output of ps or ls -l, each space
counts as a separator, and the field numbers do not match the columns you see.
sort, uniq)| Command | Effect |
|---|---|
sort |
Sort the lines alphabetically |
sort -n |
Sort the lines as numbers (9 before 18) |
sort -r |
Sort the lines in reverse order (combine with -n: -nr) |
sort -h |
Sort sizes like du -h prints them (900K before 1.2G) |
sort | uniq |
Sort the lines and remove duplicates |
uniq -c |
Remove duplicates and count how many times each line came |
uniq only removes duplicates that are next to each other. That is why it
almost always comes after sort.
sort | uniq -c | sort -nr)This pipeline answers “which ones, and how many of each?” about any list:
$> cat /etc/passwd | cut -d : -f 7 | sort | uniq -c | sort -nr
28 /usr/sbin/nologin
3 /bin/bash
1 /bin/sync
1 /bin/false
cut keeps the column you want to count, here the login shell of each user.sort puts identical lines next to each other.uniq -c replaces each group of identical lines by one line, with its count.sort -nr puts the largest count first.Add | head -n 5 at the end to keep only the top five. The Unix plumbing
exercise uses it to find which usernames attackers try most
often on your server.
du, sort -h)du (disk usage) shows how much space files and directories take. -s
prints one total per directory instead of a line for every file inside it, and
-h prints sizes a human can read, like 68M or 1.1G. sort -h sorts those
sizes in the right order:
$> sudo du -sh /var/* | sort -h | tail -n 5
4.0M /var/backups
24M /var/www
68M /var/cache
312M /var/log
1.1G /var/lib
The biggest directory comes last. Run the same command on it (sudo du -sh /var/lib/* | ...) to find out what inside it takes the space.
nginx writes one line per request it answers in /var/log/nginx/access.log:
203.0.113.42 - - [05/Nov/2026:14:03:12 +0100] "GET /about HTTP/1.1" 200 1534 "-" "Mozilla/5.0 ..."
Separated by spaces, column 1 is the IP address of the client, column 7 the path
it asked for, and column 9 the status code of the response (the date counts as
two columns, because it contains a space). With
cut and the counting
pipeline, you can ask:
# Which IP addresses send the most requests?
$> sudo cat /var/log/nginx/access.log | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head -n 5
# Which status codes does the server answer with, and how often?
$> sudo cat /var/log/nginx/access.log | cut -d ' ' -f 9 | sort | uniq -c | sort -nr
# Which paths are asked for but not found?
$> sudo cat /var/log/nginx/access.log | grep ' 404 ' | cut -d ' ' -f 7 | sort | uniq -c | sort -nr | head -n 5
Bots sometimes send requests that nginx cannot read, and their lines have fewer columns. A few odd values in the counts usually come from those lines.
less)Read a long output page by page, as with a file (type q to quit):
$> ps -ef | less
$> apt list --installed | less
tail -f)tail -f prints the last lines of a file, then keeps printing new lines as they
are added, until you press Ctrl-C. Pipe it
into grep to only see the lines you care about:
$> sudo tail -f /var/log/nginx/access.log | grep 404
xargs)A pipe sends text, not arguments. Some commands only use their arguments and
ignore the text you send them: code opens the files you name, in Visual Studio
Code. xargs reads the lines it receives, and runs the command you give it with
those lines added as arguments:
$> git diff --name-only | xargs code
$> grep -Rl TODO . | xargs code
The first command opens every file you have modified but not yet staged with
git add: git diff --name-only prints their paths, one per line. The second
opens every file containing the word “TODO”: grep -l prints the name of each
file that matches instead of the matching lines.
git diff example from the root directory of your repository: the
paths Git prints start from there.xargs splits what it receives on spaces too, so a file name containing a
space becomes two arguments, neither of which exists.code command must first be installed from Visual Studio Code:
open the Command Palette and run Shell Command: Install ‘code’ command in
PATH.pbcopy, clip.exe)Send what a command prints to your clipboard, then paste it anywhere with
Cmd-V (macOS) or Ctrl-V (Windows):
# On macOS
$> cat ~/.ssh/id_ed25519.pub | pbcopy
# In the WSL on Windows
$> cat ~/.ssh/id_ed25519.pub | clip.exe
This is the safest way to copy a long value, such as a public key: selecting it with the mouse in the terminal can miss a character or add a line break.
sudo in a pipelinesudo only applies to the command right after it, not to the whole pipeline.
Put it before the command that needs it:
$> sudo journalctl -u ssh | grep Invalid
Here, journalctl runs as root because it needs to read the system logs, and
grep runs as you, which is enough to filter what it receives.
A redirection (>) is done by your shell, which runs as you, so
sudo echo foo > /etc/foo fails with Permission denied. Use
tee instead.
./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
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.
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"
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