Terminal Cheat Sheet
Last updated
Navigation
Move around the filesystem and see where you are.
| Command | What it does |
|---|---|
pwd | Print the current working directory |
ls | List files in the current directory |
ls -la | List all files (including hidden) with details |
cd dir | Change into dir |
cd .. | Go up one directory |
cd ~ | Go to your home directory |
cd - | Go back to the previous directory |
tree | Show the directory tree (if installed) |
Files & directories
Create, copy, move, and delete files and folders.
| Command | What it does |
|---|---|
touch file.txt | Create an empty file (or update its timestamp) |
mkdir dir | Create a directory |
mkdir -p a/b/c | Create nested directories, no error if they exist |
cp src dst | Copy a file |
cp -r src dst | Copy a directory recursively |
mv src dst | Move or rename a file |
rm file | Delete a file |
rm -r dir | Delete a directory and its contents |
rm -rf dir | Force-delete recursively (no prompt - careful) |
Viewing files
Print or page through file contents.
| Command | What it does |
|---|---|
cat file | Print the whole file |
less file | Scroll through a file page by page (q to quit) |
head file | Show the first 10 lines |
head -n 20 file | Show the first 20 lines |
tail file | Show the last 10 lines |
tail -f log | Follow a file as new lines are appended |
wc -l file | Count the number of lines |
Searching
Find files by name and search inside their contents.
| Command | What it does |
|---|---|
grep "text" file | Find lines matching text in a file |
grep -r "text" . | Search recursively from the current directory |
grep -i "text" file | Case-insensitive search |
grep -n "text" file | Show matching line numbers |
find . -name "*.js" | Find files by name pattern |
find . -type d | Find only directories |
find . -size +1M | Find files larger than 1 MB |
Pipes & redirection
Combine commands and control where their input and output go.
| Command | What it does |
|---|---|
cmd1 | cmd2 | Pipe the output of cmd1 into cmd2 |
cmd > file | Redirect output to a file (overwrites it) |
cmd >> file | Append output to a file |
cmd < file | Read input from a file |
cmd 2> errors.txt | Redirect only error output (stderr) |
cmd > out.txt 2>&1 | Send both output and errors to one file |
cmd | tee file | Print output and write it to a file at once |
ls | grep .txt | Example: list files, keep only the .txt ones |
Permissions
Change who can read, write, or run a file.
| Command | What it does |
|---|---|
ls -l | Show permissions, owner, and size of each file |
chmod +x script.sh | Make a file executable |
chmod 644 file | Set rw-r--r-- (owner writes, others read) |
chmod 755 file | Set rwxr-xr-x (owner all, others read/run) |
chown user file | Change the file's owner |
chown user:group file | Change owner and group |
sudo cmd | Run a command as the superuser |
Process control
Inspect, pause, and stop running programs.
| Command | What it does |
|---|---|
ps aux | List all running processes |
top | Live view of processes and resource usage |
kill PID | Send a terminate signal to a process by ID |
kill -9 PID | Force-kill a process |
cmd & | Run a command in the background |
jobs | List background jobs in this shell |
fg | Bring a background job to the foreground |
bg | Resume a suspended job in the background |
History & shortcuts
Recall past commands and control the current one.
| Command | What it does |
|---|---|
history | Show recently run commands |
!! | Re-run the previous command |
!42 | Re-run command number 42 from history |
Ctrl+R | Search backward through command history |
Ctrl+C | Cancel the running command |
Ctrl+Z | Suspend the running command |
Ctrl+L | Clear the screen (same as clear) |
Ctrl+A / Ctrl+E | Jump to start / end of the line |
Environment & misc
Variables, locating commands, and getting help.
| Command | What it does |
|---|---|
echo $HOME | Print an environment variable |
export VAR=value | Set an environment variable for this session |
which python | Show the full path of a command |
man ls | Open the manual page for a command |
ls --help | Show quick usage for a command |
alias ll="ls -la" | Create a shortcut for a command |
clear | Clear the terminal screen |
Every command you reach for at the prompt, on one page. This terminal cheat sheet is a quick reference for the interactive shell - moving around the filesystem, working with files, wiring commands together with pipes and redirection, and managing running processes.
The commands here are standard on bash and zsh, so they work the same on Linux and macOS. Copy what you need, or try them live in the terminal playground - a real shell in your browser, nothing to install.
Terminal cheat sheet FAQ
Is this terminal cheat sheet free?
What shell is this cheat sheet for - bash or zsh?
What does the pipe | do?
ls | grep .txt lists files and then filters that list to only the names containing .txt - no temporary file needed.