Cheat sheetDev

Terminal basics

The terminal commands you'll actually use daily — navigation, files, search, pipes, and process control in one page.

Last updated

Moving around

CommandWhat it does
pwdPrints where you actually are — the answer once relative paths stop making sense.
ls -laLists hidden dotfiles and permissions too — plain ls hides configs like .env and .git.
ls -lhSame listing, but sizes shown as 4.2K instead of 4200 — for hunting a huge file.
cd -Jumps back to wherever you just were — the undo button for cd.
cd ~Home, no matter how deep you've wandered.
find . -type dLists every folder under here — the map before you go searching inside them.

Files

CommandWhat it does
cp file.txt backup.txtDuplicates a file — the original stays untouched.
cp -r src/ dest/Copies a whole folder tree — plain cp refuses directories outright.
mv old.txt new.txtRenames a file — same command whether you're moving it or just renaming it.
rm file.txtDeletes for good — no trash can to fish it back out of.
rm -r folder/Deletes a folder and everything inside it — double-check the path first.
mkdir -p a/b/cCreates nested folders in one shot, even if a and b don't exist yet.
touch file.txtCreates an empty file, or just updates its timestamp if it already exists.
cat file.txtDumps the whole file to the screen — fine for short files, useless for long ones.
less file.txtOpens a file you can scroll and search without it flooding your terminal.
head -n 20 file.txtFirst 20 lines — the fast way to check what a file even is.
tail -f log.txtFollows a file as it grows — the standard way to watch a log live.

Searching

CommandWhat it does
grep -r "TODO" .Recursively searches file contents — finds a string no matter how deep it's buried.
grep -ri "error" log.txtCase-insensitive search — catches Error, ERROR, and error alike.
find . -name "*.env"Finds files by name pattern anywhere below here, not just the current folder.
which nodePrints the exact path of the executable that would run — for chasing down 'command not found' or the wrong version.
historyEvery command you've typed this session, numbered — !42 reruns line 42.
history | grep dockerSearches your command history instead of scrolling back through it.

Pipes & redirection

CommandWhat it does
cmd1 | cmd2Feeds the output of one command straight into the next — the glue of the shell.
cmd > file.txtWrites output to a file, overwriting whatever was already there.
cmd >> file.txtAppends output to a file instead of overwriting it.
cmd > out.log 2>&1Sends both normal output and errors into the same file.
find . -name "*.log" | xargs wc -lTurns a list of results into arguments for another command. To delete instead, prefer find -delete — piping to rm breaks on paths with spaces.
cmd | tee file.txtShows output on screen AND saves it to a file at the same time.

Processes & help

CommandWhat it does
ps auxLists every running process with its PID — what you need before you can kill anything.
kill 1234Asks a process to shut down gracefully, by its PID.
kill -9 1234Forces a stuck process to die immediately — the last resort, not the first.
topA live, updating view of what's eating your CPU and memory right now.
Ctrl+CStops whatever's running in the foreground — the universal 'get me out of this'.
Ctrl+ZPauses the foreground job and hands the prompt back — the process is frozen, not stopped.
fgResumes whatever you just paused with Ctrl+Z, back in the foreground.
man lsOpens the full manual for a command, with every flag it supports.
ls --helpA quick flag summary without leaving your prompt — faster than man for a refresher.

Worth remembering

  • →Tab-completion isn't optional — it saves you from typos in paths and surfaces files you forgot existed.
  • →rm has no undo — for anything that matters, cp -r it somewhere safe first, or reach for a trash-cli instead.

Questions

Why does cd in a script not change my shell?

Because a script runs in a child process, and a child cannot change its parent's directory. Source the script instead of executing it if you want the change to persist in your session.

What is the difference between > and >>?

A single arrow truncates the file before writing; a double appends. Redirecting to a file you meant to append to is one of the few terminal mistakes with no undo.

How do I redirect errors as well as output?

2>&1 sends stderr to wherever stdout is going, and it must come after the redirect to be effective. Writing it first points stderr at the terminal, which is rarely intended.

Why does my command not exist in a script?

The environment differs from your interactive shell — no profile is sourced, so PATH is minimal and aliases do not exist. Use absolute paths, or set the environment explicitly at the top.

What is the fastest way to fix a mistyped command?

Ctrl-A moves to the start of the line and Ctrl-E to the end, which is faster than holding an arrow key. Both work in most shells and in many text inputs elsewhere.

Related in Cheat sheets