Linux cheat sheet

All 157 commands of the IVski Linux Playground on one page — what each does and an example, grouped by topic. Print it or save it as a PDF.

Getting Started

clear — clear the terminal screen
clear
date — print or set the system date and time
date
man — an interface to the system reference manuals
man ls
uname — print system information
uname -a
uptime — Tell how long the system has been running.
uptime
whoami — print effective user name
whoami

Navigation

cd — Change the shell working directory.
cd ~
ls — list directory contents
ls -l
pwd — Print the name of the current working directory.
pwd

Files & Directories

cat — concatenate files and print on the standard output
cat -n myfile.txt
cp — copy files and directories
cp -i file.txt dest/
ln — make links between files
ln -s TARGET NAME
mkdir — make directories
mkdir -p ~/site/v1 ~/site/v2
mv — move (rename) files
mv -i file.txt dest/
readlink — print resolved symbolic links or canonical file names
readlink ~/system-logs
rm — remove files or directories
rm -I *.txt
rmdir — remove empty directories
rmdir emptydirectory/

Reading Files

diff — compare files line by line
diff -i a.txt b.txt
grep — print lines that match patterns
grep -l 'word' *.txt
head — output the first part of files
head -1 data.csv
less — display the contents of a file in a terminal
less -N /etc/passwd
tail — output the last part of files
tail -n +2 data.csv
wc — print newline, word, and byte counts for each file
wc -l *.txt

Permissions

apt-get — APT package handling utility -- command-line interface
sudo apt-get update
chgrp —
chgrp -R www-data /var/www/
chmod — change file mode bits
chmod -R 755 directory/
chown — change file owner and group
chown -R student:student directory/
getfacl — get file access control lists
getfacl /srv/reports/q3.txt
groupadd — create a new group
sudo groupadd devs
groups — print the groups a user is in
groups
setfacl — set file access control lists
setfacl -d -m u:ana:rw /srv/reports
stat — display file or file system status
stat file.txt
su — run a command with substitute user and group ID
su -
touch — change file timestamps
touch draft.txt
umask — Display or set file mode mask.
umask -S

Text Processing

awk — pattern scanning and processing language
awk -F, '{print $2}' data.csv
command — Execute a simple command or display information about commands.
command > file.txt
cut — remove sections from each line of files
cut -c5- file.txt
jq — Command-line JSON processor
jq -r '.servers[].name' ~/json/servers.json
sed — stream editor for filtering and transforming text
sed -n '5p' file.txt
sort — sort lines of text files
sort -r file.txt
tr — translate or delete characters
echo 'hello' | tr 'a-z' 'A-Z'
uniq — report or omit repeated lines
sort file.txt | uniq

Processes

bg — Move jobs to the background.
bg %1
disown — Remove jobs from current shell.
disown %1
dmesg — print or control the kernel ring buffer
sudo dmesg | grep -i "killed process"
fg — Move job to the foreground.
fg
jobs — Display status of jobs.
jobs
kill — Send a signal to a job.
kill -l
killall — kill processes by name
killall nginx
nice — run a program with modified scheduling priority
nice -n 19 tar -czf ~/docs.tar.gz ~/documents
nohup — run a command immune to hangups, with output to a non-tty
nohup command &
pgrep — look up, signal, or wait for processes based on name and other attributes
pgrep nginx
pkill — look up, signal, or wait for processes based on name and other attributes
pkill -u student
ps — report a snapshot of the current processes.
ps -ef
pstree — display a tree of processes
pstree
renice — alter priority of running processes
renice -n 10 -p 3412
sleep — delay for a specified amount of time
sleep 5
top — display Linux processes
top -b -n 1 | head -20
watch — execute a program periodically, showing output fullscreen
watch -n 5 free -h

Users & Groups

gpasswd — administer /etc/group and /etc/gshadow
sudo gpasswd -a user group
groupdel — delete a group
sudo groupdel developers
id — print real and effective user and group IDs
id
last — display login, logout and reboot information
last
lastlog —
lastlog
passwd — change user password
passwd
useradd — create a new user or update default new user information
sudo useradd -m -s /bin/bash -c "Full Name" username
userdel — delete a user account and related files
sudo userdel -r username
usermod — modify a user account
sudo usermod -L username
w — Show who is logged on and what they are doing.
w
who — show who is logged on
who

Networking

curl — transfer a URL
curl -L https://short.url
dig — DNS lookup utility
dig google.com
hostname — show or set the system's host name
hostname -I
ifconfig — configure a network interface
ifconfig
ip — show / manipulate routing, network devices, interfaces and tunnels
ip addr
netstat — Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
netstat -tlnp
nslookup — query Internet name servers interactively
nslookup google.com
ping — send ICMP ECHO_REQUEST to network hosts
ping -i 2 host
python3 — an interpreted, interactive, object-oriented programming language
curl -s https://jsonplaceholder.typicode.com/posts/1 | python3 -m json.tool
ss — another utility to investigate sockets
ss -tlnp
wget — The non-interactive network downloader.
wget -i urls.txt

Archives & Backups

gunzip — compress or expand files
gunzip file.txt.gz
gzip — compress or expand files
gzip -k file.txt
rsync — a fast, versatile, local/remote file-copying tool
rsync -av ~/site/ ~/site-copy/
tar — an archiving utility
tar -xvf archive.tar
unzip — list, test and extract compressed files in a ZIP archive
unzip -l archive.zip
zip — package and compress (archive) files
zip -9 archive.zip files/

Disk & Memory

blkid — locate/print block device attributes
sudo blkid -s UUID -o value /dev/sdb1
df — report file system space usage
df -h
du — estimate file space usage
du -h
fallocate — preallocate or deallocate space to a file
sudo fallocate -l 2G /swapfile
fdisk — manipulate disk partition table
sudo fdisk -l /dev/sdb
findmnt — find a filesystem
sudo findmnt --verify
free — Display amount of free and used memory in the system
free -m
lsblk — list block devices
lsblk
mkfs.ext4 — create an ext2/ext3/ext4 file system
sudo mkfs.ext4 -L data /dev/sdb1
mkswap — set up a Linux swap area
sudo mkswap /swapfile
mount — mount a filesystem
sudo mount -a
ncdu — NCurses Disk Usage
ncdu /var
reboot — Power off, reboot, or halt the machine
sudo reboot
swapon — enable/disable devices and files for paging and swapping
swapon --show
umount — unmount filesystems
sudo umount /srv/data
vmstat — Report virtual memory statistics
vmstat 1 5

Shell & Environment

alias — Define or display aliases.
alias ll='ls -la'
env — run a program in a modified environment
env
export — Set export attribute for shell variables.
export EDITOR=nano
history — Display or manipulate the history list.
history -c
printenv — print all or part of environment
printenv
source — Execute commands from a file in the current shell.
source ~/.bashrc
type — Display information about command type.
type ls
unalias — Remove each NAME from the list of defined aliases.
unalias ll
whereis — locate the binary, source, and manual page files for a command
whereis ls
which — locate a command
which ls

Shell Scripting

bash — GNU Bourne-Again SHell
bash myscript.sh
break — Exit for, while, or until loops.
break
continue — Resume for, while, or until loops.
continue
echo — Write arguments to the standard output.
echo "files in /etc: $(wc -l < "$TMP")"
exit — Exit the shell.
exit 1
local — Define local variables.
local src="$1"
mktemp — create a temporary file or directory
mktemp -d
set — Set or unset values of shell options and positional parameters.
set -e
sh — command interpreter (shell)
sh myscript.sh
shift — Shift positional parameters.
shift
trap — Trap signals and other events.
trap -p

Advanced Commands

crontab — maintain crontab files for individual users (Vixie Cron)
crontab -e
dd — convert and copy a file
dd
find — search for files in a directory hierarchy
find . -type f
nano — Nano's ANOther text editor, inspired by Pico
nano -l file.txt
scp — OpenSSH secure file copy
scp -r directory/ user@host:/
ssh — OpenSSH remote login client
ssh -p 2222 username@hostname
ssh-copy-id — use locally available keys to authorise logins on a remote machine
ssh-copy-id username@hostname
ssh-keygen — OpenSSH authentication key utility
ssh-keygen -t ed25519 -C "your@email.com"
vim — Vi IMproved, a programmer's text editor
vim filename.txt
xargs — build and execute command lines from standard input
xargs -n 1 command

Package Management

apt — command-line interface
sudo apt install -f
apt-mark — show, set and unset various settings for a package
sudo apt-mark hold htop
dpkg — package manager for Debian
dpkg -l

Git & Version Control

git — the stupid content tracker
git add -A

Services & systemd

journalctl — Print log entries from the systemd journal
journalctl -r
systemctl — Control the systemd system and service manager
systemctl status sshd
systemd-analyze — Analyze and debug system manager
systemd-analyze calendar 'Mon..Fri 09:00'

Server Setup

hostnamectl — Control the system hostname
hostnamectl
logrotate — rotates, compresses, and mails system logs
sudo logrotate -d /etc/logrotate.d/myapp
printf — Formats and prints ARGUMENTS under control of the FORMAT.
printf 'Unattended-Upgrade::Automatic-Reboot "true";\nUnattended-Upgrade::Automatic-Reboot-Time "02:00";\n' | sudo tee /etc/apt/apt.conf.d/52auto-reboot
timedatectl — Control the system time and date
timedatectl
unattended-upgrade — automatic installation of security (and other) upgrades
sudo unattended-upgrade -v

Web Server (nginx)

nginx — HTTP and reverse proxy server, mail proxy server
sudo nginx -t
tee — read from standard input and write to standard output and files
command | tee output.txt

Server Security

certbot — Automatically configure HTTPS using Let's Encrypt
sudo certbot --nginx -d aroma.example -d www.aroma.example
fail2ban-client — configure and control the server
sudo fail2ban-client status
sshd — OpenSSH daemon
sudo sshd -t
ufw — program for managing a netfilter firewall
sudo ufw status

Databases (MariaDB)

mariadb — the MariaDB command-line tool (mysql is now a symlink to mariadb)
mariadb -u shop -p shop
mariadb-dump — a database backup program (mariadb-dump is now a symlink to mariadb-dump)
sudo mariadb-dump shop > ~/shop.sql

tmux & Screen

screen — screen manager with VT100/ANSI terminal emulation
screen -ls
tmux — terminal multiplexer
tmux a -t work