Linux cheat sheet

A quick reference for Linux with the most important commands and options. Read it online below, or download the printable PDF.

Download PDF

Working with files

Command Description
cat <file> Print file contents
less <file> Page through file contents
touch <file> Create file (or update timestamp)
cp <src> <dest> Copy a file
mv <src> <dest> Move / rename a file
rm <file> Remove file
head <file> Print first 10 lines of file
tail <file> Print last 10 lines of file
tail -f <file> Follow the file as it grows

Working with directories

Command Description
ls List files in this directory
ls -l List files with details
ls -al List all files with details
pwd Print the working directory
cd <dir> Change to <dir>
cd .. Go up one directory
mkdir <dir> Create directory
mkdir -p <dir> Create directory, with parents
cp -r <src> <dest> Copy directory and contents
rmdir <dir> Remove an empty directory
rm -r <dir> Remove directory and contents

Path shorthands

Path Refers to
. Current directory
.. Parent directory
~ Home directory
Command Description
ln -s <target> <link> Create symbolic link
ln <target> <link> Create hard link
realpath <file> Print absolute path of file

Searching files and content

Command Description
grep <pattern> <file> Search for pattern in file(s)
grep -r <pattern> <dir> Search recursively in a directory
grep -i <pattern> <file> Search case-insensitively
grep -v <pattern> <file> Show lines not matching pattern
grep -n <pattern> <file> Search and print line numbers
find <dir> -name '<pattern>' Find by name (quote the pattern)
find <dir> -type f Find regular files
find <dir> -type d Find directories

Redirecting input and output

Command Description
<cmd> > <file> Write output to file (overwrite)
<cmd> >> <file> Append output to file
<cmd> < <file> Redirect input from file
<cmd> 2> <file> Redirect stderr to file
<cmd> &> <file> Redirect stdout and stderr

Pipes

Command Description
<cmd1> | <cmd2> Pipe stdout to next cmd
<cmd1> |& <cmd2> Pipe stdout and stderr onward

&> and |& are bash/zsh extensions. In POSIX sh, use > <file> 2>&1 and 2>&1 |.

Processing text

Command Description
sort Sort lines alphabetically
sort -u Sort lines and remove duplicates
uniq -c Count repeated adjacent lines
wc -l Count lines
cut -d: -f1 Print first colon-separated field
sed 's/<old>/<new>/g' Replace <old> with <new>
awk '{print $1}' Print the first field
tee <file> Write stdout to file and screen
xargs <cmd> Build a command from stdin

Archives and compression

Command Description
tar -czf <archive> <dir> Create a gzipped tar archive
tar -xzf <archive> Extract a gzipped tar archive
gzip <file> Compress a file
gunzip <file.gz> Decompress a file
zless <file.gz> Read without decompressing
zip -r <archive> <dir> Create a zip archive
unzip <archive> Extract a zip archive

Glob patterns

Pattern Matches
* Any number of characters
? Exactly one character
[abc] One character from the set
[!abc] One character not in the set

Quote them ('*.txt') to stop the shell expanding them.

Chaining commands

Command Description
<cmd1>; <cmd2> Run both, regardless of result
<cmd1> && <cmd2> Only if the first succeeded
<cmd1> || <cmd2> Only if the first failed

Brace expansion (bash/zsh)

Pattern Expands to
file{a,b} filea fileb
day{1..3} day1 day2 day3

Shell environment

Command Description
export VAR=<val> Set for this shell and children
echo $VAR Print a variable
env List all environment variables
$PATH Where commands are looked up
. <file> Run a file in the current shell
source <file> Same, but only in bash/zsh
alias ll='ls -al' Create a shortcut for a command

Command line shortcuts

Shortcut Description
Ctrl + C Stop current command
Ctrl + Z Suspend current command
Ctrl + D End of input / exit shell
Ctrl + A Go to beginning of line
Ctrl + E Go to end of line
Ctrl + K Cut to end of line
Ctrl + U Cut to beginning of line
Ctrl + W Cut previous word
Ctrl + R Search command history
Ctrl + L Clear screen

Privilege escalation

Command Description
sudo <cmd> Run command as superuser (root)
sudo -i Start a shell as root
su Switch to superuser (root)
su <user> Switch to user <user>

Changing permissions and ownership

Command Description
chmod <mode> <file> Change file permissions
chmod u+x <file> Add execute for the owner
chmod 644 <file> Owner read/write, others read
chmod -R g+w <dir> Add group write, recursively
chown <owner> <file> Change file owner
chown -R <owner> <dir> Change owner recursively
chown <owner>:<group> <file> Change file owner and group
chgrp <group> <file> Change file group

Permission bits

Bit Description
4 Read (r)
2 Write (w)
1 Execute (x)

Digits are owner, group, everyone else. 640 means read/write for owner, read for group, none for everyone else.

Permission classes

Class Description
u Owner
g Group
o Everyone else
a All

Users and groups

Command Description
whoami Show current user name
id Show current user and groups
groups <user> Show a user's groups
useradd -m <user> Create user with home directory
userdel -r <user> Delete user and home directory
passwd <user> Set a user's password
groupadd <group> Create a group
usermod -aG <group> <user> Add a user to a group

Managing processes

Command Description
ps List processes
ps aux List all processes (BSD style)
top Display processes in real time
pgrep <name> Find process IDs by name
kill <pid> Kill process
kill -9 <pid> Force kill (last resort)
pkill <name> Kill process with name

Job control

Command Description
<cmd> & Run in the background
jobs List background jobs
fg %1 Bring job 1 to the foreground
bg %1 Resume job 1 in the background
nohup <cmd> & Keep running after logout

System information

Command Description
df -h Show disk usage, human readable
du -sh <dir> Show total size of a directory
free -h Show memory usage
uptime Show uptime
uname -a Show all system information

Services (systemd)

Command Description
systemctl status <unit> Show the status of a service
systemctl start <unit> Start a service
systemctl stop <unit> Stop a service
systemctl restart <unit> Restart a service
systemctl reload <unit> Reload config without restarting
systemctl enable --now <unit> Start now and on every boot
systemctl disable <unit> Do not start on boot
systemctl daemon-reload Reload unit files after editing
journalctl -u <unit> Show the logs of a service
journalctl -u <unit> -f Follow the logs of a service
journalctl -b Show logs since the last boot

Shutting down and rebooting

Command Description
shutdown now Power off the system immediately
shutdown 16:30 Power off the system at 16:30
shutdown -c Cancel a scheduled shutdown
reboot Reboot system
poweroff Power off system

SSH keys and connections

Command Description
ssh-keygen Generate SSH key pair
ssh-add <key> Add SSH key to SSH agent
ssh-add -l List SSH keys in SSH agent
ssh-copy-id <user>@<host> Copy SSH key to <host>
ssh <host> Connect as the current user
ssh <user>@<host> Connect to <host> as <user>

Copying files between hosts

Command Description
scp <src> <host>:<dest> Copy local file to <host>
scp <host>:<src> <dest> Copy file from <host> to local

Syncing files with rsync

Command Description
rsync -av <src> <dest> Sync, preserving attributes
rsync -av <src> <host>:<dest> Sync to a remote host
rsync -av --delete <src> <dest> Sync and delete extra files

Networking

Command Description
ip a Show interfaces and addresses
ip r Show the routing table
ss -tulpn List listening TCP and UDP ports
ping <host> Check whether a host responds
traceroute <host> Show the network route to a host
dig <domain> Look up DNS records
curl <url> Fetch a URL to stdout
curl -O <url> Download a URL to a file
wget <url> Download a file

Package management (Debian/Ubuntu)

Command Description
apt install <pkg> Install package
apt remove <pkg> Remove package
apt purge <pkg> Remove package and config files
apt update Update package list
apt upgrade Upgrade packages
apt search <term> Search for package

Getting help and finding commands

Command Description
man <cmd> Display manual page for <cmd>
man -k <term> Search manual page descriptions
history Show previously run commands
command -v <cmd> Find path or builtin for <cmd>

Filesystem layout

Directory Description
/ Root
/dev Device files
/etc System configuration files
/home User home directories
/opt Optional or third-party software
/proc Process and kernel information
/root Home directory of the root user
/tmp Temporary files
/usr Installed software, read-only
/usr/bin Executable programs
/usr/lib Shared libraries and helpers
/usr/sbin System administration programs
/var Variable data
/var/log Log files

/bin, /sbin and /lib are symlinks into /usr.