pwdWhere am I? Prints the folder you're in.
Cheat sheet
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.
Nothing matches that. Try fewer words, or a shorter one.
pwdWhere am I? Prints the folder you're in.
cd ~/projectsGo to a folder.
cd -Jump back to the folder you were in a moment ago. Handy for bouncing between two places.
cdStraight back to your home folder, from anywhere.
ls -lah ~/projectsEverything in a folder, hidden files included, with sizes a human can read.
-l-a-hls -lt ~/projects | headThe ten most recently changed things in a folder, newest first.
cat notes.txtPrint a whole file. Best for short ones.
less notes.txtRead 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.txtJust the first 20 lines.
tail -n 50 notes.txtJust the last 50 lines. Usually where the interesting bit of a log is.
tail -F notes.txtWatch a log as it grows. Ctrl+C stops it.
-F-f would go quiet)wc -l notes.txtCount the lines in a file.
file notes.txtWhat kind of file this really is, whatever its name says.
cp notes.txt notes.txt.bakMake a quick backup copy before you edit something.
cp -a ~/projects ~/projects.bakCopy a whole folder, keeping owners, permissions and dates exactly as they were.
-amv notes.txt ~/projects/Move a file into a folder. The same command renames: mv old new.
mkdir -p ~/projectsMake a folder, and any missing folders above it. No complaint if it's already there.
-prm -i notes.txtDelete a file, asking first. There's no recycle bin.
rm -r ~/projectsDelete a folder and everything in it. No undo, so read it twice before you press Enter.
-rfind ~/projects -name '*.log'Find files by name, in a folder and everything below it.
-name* for find)find ~/projects -type f -mtime -7Files changed in the last few days.
-type f-mtime -7find ~/projects -type f -size +100MFiles bigger than 100 MB. The usual suspects when a disk fills up.
find ~/projects -name '*.tmp' -deleteDelete everything that matches. Run it once without -delete first to see what it would take.
grep -rn error ~/projectsSearch inside every file in a folder.
-r-ngrep -i error notes.txtSearch a file, ignoring upper and lower case.
grep -v error notes.txtEvery line that does NOT match. Great for filtering out noise.
grep -c error notes.txtCount the matching lines instead of printing them.
which nginxWhere a program lives, i.e. which one runs when you type its name.
chmod 644 notes.txtThe usual for a file: you can read and write it, everyone else can only read it.
64chmod 600 notes.txtOnly you can read or write it. The right setting for private keys and anything with a password in it.
chmod +x notes.txtMake a script runnable.
chown -R deploy: ~/projectsGive a folder, and everything in it, to a user and their group. Needs sudo unless it's yours already.
-Ruser:stat notes.txtPermissions, owner, size and every timestamp for one file.
id deployWhich groups a user is in.
sudo -iu deployBecome another user, with their environment, until you type exit.
df -hHow full each disk is.
du -sh ~/projectsHow big a folder is, all in.
-sdu -h -d 1 ~/projects | sort -hWhat's taking the space in a folder, biggest last. Run it again on the biggest one to dig down.
-d 1sort -hlsblkDisks, partitions and where they're mounted.
pgrep -a nginxIs it running? Shows the process ID and full command line of everything with that name.
ps aux --sort=-%mem | headThe processes using the most memory (GNU).
topA live view of what's busy. Press M to sort by memory, P by CPU, q to quit.
kill 4242Ask a process to stop. It gets a chance to tidy up first.
kill -9 4242Make it stop right now. A last resort: the process can't clean up after itself.
pkill nginxAsk everything with that name to stop.
sudo ss -tlnpWhat's listening for network connections, and which program it is.
-t-l-n-ptar -czf backup.tar.gz ~/projectsPack a folder into a compressed archive.
-c-z-ftar -tzf backup.tar.gzList what's inside without unpacking it.
-ttar -xzf backup.tar.gzUnpack an archive into the current folder.
-xtar -xzf backup.tar.gz -C ~/projectsUnpack into a different folder (it has to exist already).
-Cgzip -k notes.txtCompress one file, keeping the original. gunzip undoes it.
-ksort notes.txt | uniq -c | sort -rn | headThe most common lines in a file, with counts. Brilliant for log files.
awk '{print $1}' notes.txtJust the first column of each line (split on spaces). Change $1 for other columns.
cut -d: -f1 /etc/passwdSplit each line on a character and keep one field. This one lists every user account.
-d:-f1sed -i.bak 's/old/new/g' notes.txtReplace text everywhere in a file, keeping the original as a .bak.
-i.bakgdiff -u notes.txt.bak notes.txtWhat changed between two files.
history | grep errorFind a command you ran before. Or press Ctrl+R and start typing.