All cheat sheets

Cheat sheet

Linux shell essentials

The everyday commands for getting around a Linux box, finding things, and working out where the disk space went.

Nothing you type here is sent or saved. Privacy policy

Written for bash on a GNU/Linux box (Debian, Ubuntu, Fedora, Rocky and friends). Most of it works on a Mac too, but a few flags are GNU-only, and those are marked.

Getting around

pwd

Where am I? Prints the folder you're in.

cd ~/projects

Go to a folder.

cd -

Jump back to the folder you were in a moment ago. Handy for bouncing between two places.

cd

Straight back to your home folder, from anywhere.

Looking at files

ls -lah ~/projects

Everything in a folder, hidden files included, with sizes a human can read.

-l
one per line, with permissions, owner, size and date
-a
include hidden files (the ones starting with a dot)
-h
sizes in K, M and G instead of bytes
ls -lt ~/projects | head

The ten most recently changed things in a folder, newest first.

cat notes.txt

Print a whole file. Best for short ones.

less notes.txt

Read a long file a screen at a time. Type / and a word to search, n for the next match, q to quit.

head -n 20 notes.txt

Just the first 20 lines.

tail -n 50 notes.txt

Just the last 50 lines. Usually where the interesting bit of a log is.

tail -F notes.txt

Watch a log as it grows. Ctrl+C stops it.

-F
keeps following by name, so it carries on after the log is rotated (-f would go quiet)
wc -l notes.txt

Count the lines in a file.

file notes.txt

What kind of file this really is, whatever its name says.

Copying, moving and deleting

cp notes.txt notes.txt.bak

Make a quick backup copy before you edit something.

cp -a ~/projects ~/projects.bak

Copy a whole folder, keeping owners, permissions and dates exactly as they were.

-a
archive mode: copy everything inside, and keep all the details
mv notes.txt ~/projects/

Move a file into a folder. The same command renames: mv old new.

mkdir -p ~/projects

Make a folder, and any missing folders above it. No complaint if it's already there.

-p
create parents as needed
rm -i notes.txt

Delete a file, asking first. There's no recycle bin.

rm -r ~/projects

Delete a folder and everything in it. No undo, so read it twice before you press Enter.

-r
recursive: the folder and everything inside it

Finding things

find ~/projects -name '*.log'

Find files by name, in a folder and everything below it.

-name
match the file name (quote the pattern so the shell leaves the * for find)
find ~/projects -type f -mtime -7

Files changed in the last few days.

-type f
files only, not folders
-mtime -7
modified less than 7 days ago
find ~/projects -type f -size +100M

Files bigger than 100 MB. The usual suspects when a disk fills up.

find ~/projects -name '*.tmp' -delete

Delete everything that matches. Run it once without -delete first to see what it would take.

grep -rn error ~/projects

Search inside every file in a folder.

-r
look in every file below the folder
-n
show the line number of each match
grep -i error notes.txt

Search a file, ignoring upper and lower case.

grep -v error notes.txt

Every line that does NOT match. Great for filtering out noise.

grep -c error notes.txt

Count the matching lines instead of printing them.

which nginx

Where a program lives, i.e. which one runs when you type its name.

Permissions and owners

chmod 644 notes.txt

The usual for a file: you can read and write it, everyone else can only read it.

6
read (4) + write (2), for the owner
4
read only, for the group and then everyone else
chmod 600 notes.txt

Only you can read or write it. The right setting for private keys and anything with a password in it.

chmod +x notes.txt

Make a script runnable.

chown -R deploy: ~/projects

Give a folder, and everything in it, to a user and their group. Needs sudo unless it's yours already.

-R
recursive
user:
the colon with nothing after it means "and that user's own group"
stat notes.txt

Permissions, owner, size and every timestamp for one file.

id deploy

Which groups a user is in.

sudo -iu deploy

Become another user, with their environment, until you type exit.

Disk space

df -h

How full each disk is.

du -sh ~/projects

How big a folder is, all in.

-s
one total, not every subfolder
du -h -d 1 ~/projects | sort -h

What's taking the space in a folder, biggest last. Run it again on the biggest one to dig down.

-d 1
one level deep
sort -h
sorts human sizes (K, M, G) properly
lsblk

Disks, partitions and where they're mounted.

Processes

pgrep -a nginx

Is it running? Shows the process ID and full command line of everything with that name.

ps aux --sort=-%mem | head

The processes using the most memory (GNU).

top

A live view of what's busy. Press M to sort by memory, P by CPU, q to quit.

kill 4242

Ask a process to stop. It gets a chance to tidy up first.

kill -9 4242

Make it stop right now. A last resort: the process can't clean up after itself.

pkill nginx

Ask everything with that name to stop.

sudo ss -tlnp

What's listening for network connections, and which program it is.

-t
TCP
-l
listening only
-n
numbers, not names, for ports and addresses
-p
the program behind each one (that's why it needs sudo)

Archives

tar -czf backup.tar.gz ~/projects

Pack a folder into a compressed archive.

-c
create
-z
compress with gzip
-f
the archive's file name comes next, so keep it last in the bunch
tar -tzf backup.tar.gz

List what's inside without unpacking it.

-t
list
tar -xzf backup.tar.gz

Unpack an archive into the current folder.

-x
extract
tar -xzf backup.tar.gz -C ~/projects

Unpack into a different folder (it has to exist already).

-C
change to that folder first
gzip -k notes.txt

Compress one file, keeping the original. gunzip undoes it.

-k
keep the original

Text wrangling

sort notes.txt | uniq -c | sort -rn | head

The most common lines in a file, with counts. Brilliant for log files.

awk '{print $1}' notes.txt

Just the first column of each line (split on spaces). Change $1 for other columns.

cut -d: -f1 /etc/passwd

Split each line on a character and keep one field. This one lists every user account.

-d:
split on colons
-f1
keep the first field
sed -i.bak 's/old/new/g' notes.txt

Replace text everywhere in a file, keeping the original as a .bak.

-i.bak
edit in place, saving a backup first
g
every match on a line, not just the first
diff -u notes.txt.bak notes.txt

What changed between two files.

history | grep error

Find a command you ran before. Or press Ctrl+R and start typing.