Cheat sheets / Linux

Linux Terminal Cheat Sheet

The everyday Linux commands for moving around, reading files, searching, permissions, users, processes and archives. · Updated 2026-12-04

Keep this page open while you practise. Each table shows a command, what it does and a short example. Add --help to almost any command, or run man <command>, to see all its options.

Command What it does Example
pwd Print the folder you are in pwd
ls List files in a folder ls /var/log
ls -la List all files, including hidden ones, with details ls -la ~
ls -lh Show sizes in KB, MB, GB ls -lh /tmp
cd <dir> Change folder cd /etc
cd .. Go up one folder cd ..
cd ~ or cd Go to your home folder cd
cd - Go back to the previous folder cd -

Paths: / is the root of the file system, ~ is your home folder, . is the current folder and .. is the parent.

Files and folders

Command What it does Example
touch <file> Create an empty file (or update its time) touch notes.txt
mkdir -p <dir> Create a folder, including parent folders mkdir -p lab/scans
cp <src> <dst> Copy a file cp notes.txt backup.txt
cp -r <src> <dst> Copy a folder and everything in it cp -r lab lab-old
mv <src> <dst> Move or rename mv old.txt new.txt
rm <file> Delete a file (no recycle bin) rm temp.txt
rm -r <dir> Delete a folder and its contents rm -r lab-old
file <file> Show what type of file it really is file image.jpg

Be careful with rm -r. There is no undo.

Viewing files

Command What it does Example
cat <file> Print the whole file cat /etc/hostname
less <file> Scroll through a long file (q to quit, /word to search) less /var/log/syslog
head -n 20 <file> First 20 lines head -n 20 access.log
tail -n 20 <file> Last 20 lines tail -n 20 auth.log
tail -f <file> Follow new lines as they are written tail -f /var/log/auth.log
wc -l <file> Count lines wc -l users.txt

Searching

Command What it does Example
grep <text> <file> Show lines that contain the text grep "Failed" auth.log
grep -i Ignore upper and lower case grep -i "error" app.log
grep -r Search every file inside a folder grep -r "password" /var/www
grep -n Show line numbers grep -n "root" /etc/passwd
grep -v Show lines that do not match grep -v "^#" config.conf
grep -c Count matching lines grep -c "404" access.log
find <dir> -name <pattern> Find files by name find / -name "*.bak" 2>/dev/null
find <dir> -type d Find folders only find /home -type d
find <dir> -user <name> Find files owned by a user find / -user ali 2>/dev/null
find <dir> -perm -4000 Find files with the SUID bit find / -perm -4000 -type f 2>/dev/null
find <dir> -mtime -1 Files changed in the last day find /etc -mtime -1
which <cmd> Show where a command lives which python3

2>/dev/null hides "Permission denied" errors so the results are easier to read.

Pipes and redirection

Symbol What it does Example
| Send output of one command into the next cat auth.log | grep sshd
> Write output to a file (overwrites) ls > files.txt
>> Add output to the end of a file echo "done" >> notes.txt
2> Send errors to a file find / -name x 2> errors.txt

Permissions

Read ls -l output like this: -rwxr-x--- = type, then owner, group, others.

Letter Number On a file On a folder
r 4 Read the contents List the files
w 2 Change the contents Create or delete files inside
x 1 Run it as a program Enter it with cd
- 0 No permission No permission

Octal table

Number Letters Meaning
7 rwx read, write, execute
6 rw- read, write
5 r-x read, execute
4 r-- read only
0 --- nothing

Common modes: 644 (normal file), 600 (private file such as an SSH key), 755 (program or folder), 700 (private folder). Avoid 777: everyone can change the file.

Command What it does Example
chmod 644 <file> Set permissions with numbers chmod 600 id_rsa
chmod u+x <file> Add execute for the owner chmod u+x script.sh
chmod o-w <file> Remove write for others chmod o-w data.txt
chown <user> <file> Change the owner sudo chown ali report.txt
chown <user>:<group> <file> Change owner and group sudo chown www-data:www-data index.html
chmod -R / chown -R Apply to a folder and everything inside sudo chown -R ali:ali /home/ali

Users and privileges

Command What it does Example
whoami Show your user name whoami
id Show your user ID and groups id
sudo <cmd> Run a command as root sudo apt update
sudo -l List what you are allowed to run with sudo sudo -l
su - <user> Switch to another user su - ali
cat /etc/passwd List user accounts cat /etc/passwd
last Show recent logins last -n 10
w Show who is logged in and what they are doing w

Processes and services

Command What it does Example
ps aux List every running process ps aux | grep nginx
top Live view of processes (q to quit) top
kill <PID> Ask a process to stop kill 1234
kill -9 <PID> Force a process to stop kill -9 1234
systemctl status <svc> Show if a service is running systemctl status ssh
sudo systemctl start <svc> Start a service sudo systemctl start nginx
sudo systemctl stop <svc> Stop a service sudo systemctl stop nginx
sudo systemctl restart <svc> Restart a service sudo systemctl restart nginx
sudo systemctl enable <svc> Start a service at boot sudo systemctl enable ssh
systemctl list-units --type=service List loaded services systemctl list-units --type=service
journalctl -u <svc> Show logs for a service journalctl -u ssh --since today

On some systems the SSH service is called sshd instead of ssh.

Archives

Command What it does Example
tar -czf <out>.tar.gz <dir> Create a compressed archive tar -czf logs.tar.gz /var/log/nginx
tar -xzf <file>.tar.gz Extract a .tar.gz archive tar -xzf logs.tar.gz
tar -tzf <file>.tar.gz List what is inside, without extracting tar -tzf logs.tar.gz
zip -r <out>.zip <dir> Create a zip file zip -r lab.zip lab
unzip <file>.zip Extract a zip file unzip lab.zip
gzip <file> / gunzip <file>.gz Compress or decompress one file gunzip access.log.1.gz

Remember the tar letters: create, extract, t list, z gzip, f file name, v verbose (optional).

Handy shortcuts

Key What it does
Tab Complete a file or command name
Up arrow Show the previous command
Ctrl + C Stop the running command
Ctrl + L Clear the screen
Ctrl + R Search your command history
history List past commands

Spotted a mistake or something unclear? Report a mistake