linux commands cheat sheet: navigating, finding things, pipes and redirects, permissions and processes
Stacks · 12 min read · posted Nov 30, 2026, 4:30 pm
Most of the terminal you use day to day comes down to five things: navigating, finding things, pipes and redirects, permissions, and processes. Once those make sense, a server you've just logged into stops feeling like a black box.
This is the cheat sheet from the reel. For each of the five you get when you'd reach for it, the commands with what they print, the mistake that usually bites, and something to practise on. There's a quick lookup table first. If you don't have a terminal open yet, unix explained covers getting one on a Mac or a Windows PC, plus the very first commands.
Everything here was run on Ubuntu 24.04 in a throwaway container, as an ordinary user called you, with bash 5.2.21, GNU coreutils 9.4, GNU grep 3.11, GNU findutils 4.9.0 and procps-ng 4.0.4. The output is pasted the way it came back. The same commands were also run on macOS 26.6 (bash 3.2.57, zsh 5.9 and the BSD tools that ship with it), and wherever the Mac prints something different, the section says so.
the quick lookup
| i want to ... | run |
|---|---|
| see which folder i'm in | pwd |
| list a folder, hidden files and details included | ls -la |
| go into a folder, up one, home, back to the last one | cd src, cd .., cd, cd - |
| find every line that mentions something | grep -rn "TODO" . |
| the same, ignoring upper and lower case | grep -rni "todo" . |
| only the names of the files that match | grep -rl "TODO" . |
| find files by name | find . -name "*.py" |
| count the lines that match | grep ERROR app.log | wc -l |
| save output to a file, or add it to the end | > out.txt, >> out.txt |
| save the errors as well | > all.txt 2>&1 |
| make a script runnable | chmod +x deploy.sh |
| let only me read and write a file | chmod 600 deploy.sh |
| run something in the background | sleep 300 & |
| see what's running | jobs, ps |
| find a process id by name | pgrep -l sleep |
| stop it | kill 12, or kill %1 for a job |
| pause the program in front, then resume it | ctrl+z, then bg or fg |
the folder everything runs in
Each section starts in a fresh copy of this small project, in /home/you/app. If you want to follow along, these lines make the same one wherever you run them:
mkdir -p app/src app/tests && cd app
printf 'DEBUG=1\n' > .env
printf '# app\n' > README.md
printf 'echo deployed\n' > deploy.sh
printf '%s\n' 'INFO start' 'ERROR db down' 'INFO retry' 'ERROR timeout' 'WARN disk slow' > app.log
printf '%s\n' 'import db' '# TODO: handle errors' > src/main.py
printf '%s\n' 'def connect():' ' return None' > src/db.py
printf '%s\n' 'def test_main():' ' # todo: more cases' ' assert True' > tests/test_main.py
printf '%s\n' 'TODO: write tests' 'ship on friday' > notes.md
That gives you a hidden .env, a readme, a script, a log, two Python files in src, a test in tests and a notes file. Your dates, user name and process ids will be different from the ones below.
1. navigating
When you reach for it. You've opened a terminal, or logged into a server, and you need to know where you are and what's around you. Every other command works relative to that.
The idea. The shell always has a current folder. A path that starts with / is absolute and means the same thing from anywhere. Any other path starts from where you are. . is the current folder, .. is the one above it, and ~ is your home folder.
The commands.
$ pwd
/home/you/app
$ ls
README.md app.log deploy.sh notes.md src tests
$ ls -a
. .. .env README.md app.log deploy.sh notes.md src tests
$ ls -la
total 36
drwxr-xr-x 4 you you 4096 Nov 30 01:00 .
drwxr-x--- 1 you you 4096 Nov 30 01:00 ..
-rw-r--r-- 1 you you 8 Nov 30 01:00 .env
-rw-r--r-- 1 you you 6 Nov 30 01:00 README.md
-rw-r--r-- 1 you you 65 Nov 30 01:00 app.log
-rw-r--r-- 1 you you 14 Nov 30 01:00 deploy.sh
-rw-r--r-- 1 you you 33 Nov 30 01:00 notes.md
drwxr-xr-x 2 you you 4096 Nov 30 01:00 src
drwxr-xr-x 2 you you 4096 Nov 30 01:00 tests
Names that start with a dot are hidden, so .env only shows up once you add -a. -l gives one line per entry. Read it left to right: the permissions (section 4, and a d at the front means a folder), the link count, the owner, the group, the size in bytes, when it last changed, and the name. . and .. are listed too, because they're real entries in every folder.
Moving around:
$ cd src
$ pwd
/home/you/app/src
$ ls
db.py main.py
$ cd ..
$ pwd
/home/you/app
$ cd /tmp
$ cd -
/home/you/app
$ cd
$ pwd
/home/you
$ cd app/src
$ cd ../tests
$ pwd
/home/you/app/tests
cd - goes back to the last folder you were in and prints where that is. cd on its own goes home. cd ../tests goes up one and into tests in a single step, which is how you hop between two folders that sit side by side.
On a Mac, ls -la has the same columns, but you'll often see an @ straight after the permissions (-rw-r--r--@). It means the file has extended attributes, which macOS adds to things like downloads. Linux doesn't print it.
What bites. Spaces in names. The shell splits a command into words at every space, so cd gets two arguments:
$ mkdir "my notes"
$ cd my notes
bash: cd: too many arguments
$ cd "my notes"
$ pwd
/home/you/app/my notes
$ cd ..
$ ls
README.md app.log deploy.sh 'my notes' notes.md src tests
Quote the name, or type the first letter or two and press tab, because tab completion escapes the space for you. GNU ls puts quotes around names with spaces when it prints to a terminal, which is why 'my notes' looks like that. The Mac's bash 3.2 gives a different error for the same mistake, bash: cd: my: No such file or directory, and the Mac's ls prints the name without quotes.
Practise. In the project folder:
- From
tests, get intosrcwith onecd. Then usecd -twice and watch where you land. - List only the hidden entries. (
ls -d .*does it: here it prints just.env.) - Write down the absolute path of
main.pybefore you check it withcd srcandpwd.
2. finding things
When you reach for it. "Where is this function used", "which file sets that value", "where did I leave the TODOs", "where's the config file".
The idea. grep looks inside files for text and prints the lines that match. find walks the folder tree and prints the paths whose name, type or other details match. One searches contents, the other searches names.
The commands.
$ grep -rn "TODO" .
./notes.md:1:TODO: write tests
./src/main.py:2:# TODO: handle errors
-r goes into every folder under ., and -n adds the line number. Each line of output is file, line number and the line itself, split by colons. A few more flags you'll use all the time:
$ grep -rni "todo" .
./notes.md:1:TODO: write tests
./tests/test_main.py:2: # todo: more cases
./src/main.py:2:# TODO: handle errors
$ grep -rl "TODO" .
./notes.md
./src/main.py
$ grep -v INFO app.log
ERROR db down
ERROR timeout
WARN disk slow
$ grep -c ERROR app.log
2
-i ignores case, so the lowercase todo in the test turns up. -l prints only the file names. -v flips the match and prints the lines that don't contain the text. -c counts the matching lines instead of printing them.
Now by name:
$ find . -name "*.py"
./tests/test_main.py
./src/main.py
./src/db.py
$ find . -type d
.
./tests
./src
$ find . -iname "readme*"
./README.md
$ find . -name "*.md" -type f
./notes.md
./README.md
-type d keeps only folders and -type f only files. -iname is -name ignoring case. Put several tests in a row and a path has to pass all of them.
Neither tool sorts its output. They list files in the order the disk hands them over, so the order changes from machine to machine. On the Mac, the same find listed ./src/db.py before ./src/main.py. Add | sort (section 3) when the order matters.
What bites. Leaving the pattern unquoted. The shell expands *.py itself before find ever runs. In src there are two Python files, so find receives -name db.py main.py and gives up:
$ cd src
$ find . -name *.py
find: paths must precede expression: `main.py'
find: possible unquoted pattern after predicate `-name'?
$ find . -name "*.py"
./main.py
./db.py
The Mac's find says it differently, find: main.py: unknown primary or operator, but it's the same mistake. The worse case is a folder with exactly one match, because then there's no error at all. From tests, the unquoted *.py turns into test_main.py, and find quietly looks for that one name:
$ cd tests
$ find .. -name *.py
../tests/test_main.py
$ find .. -name "*.py"
../tests/test_main.py
../src/main.py
../src/db.py
Quote patterns every time. The same goes for grep: a search string with spaces or symbols in it needs quotes.
The other one is pointing grep at a folder without -r:
$ grep TODO src
grep: src: Is a directory
Practise.
- List every file in the project that mentions
db, names only. - Print the lines of
app.logthat are neither INFO nor WARN. (Twogrep -vjoined with a pipe, see section 3.) - Find every folder under your home folder called
node_modulesor.git, if you have any. Then run the same search without quotes from a folder that has one, and see what changes.
3. pipes and redirects
When you reach for it. The output of one command is exactly what the next command needs, or you want to keep the output instead of watching it scroll past.
The idea. Every command reads from standard input and writes to two places: standard output for results and standard error for complaints. | connects one command's output to the next command's input. > sends the output to a file and replaces whatever was in it. >> adds to the end. 2> does the same for errors.
The commands.
$ grep ERROR app.log | wc -l
2
$ grep ERROR app.log > out.txt
$ cat out.txt
ERROR db down
ERROR timeout
$ grep WARN app.log >> out.txt
$ cat out.txt
ERROR db down
ERROR timeout
WARN disk slow
$ grep ERROR app.log > out.txt
$ cat out.txt
ERROR db down
ERROR timeout
grep finds the two ERROR lines and hands them to wc -l, which counts lines. With > nothing appears on screen, because the output went into the file. >> added the WARN line under the other two. The second > replaced the whole file, so the WARN line is gone. On a Mac, wc pads its number with spaces and prints 2.
Pipes get useful when you chain them. This counts how many lines of each level are in the log:
$ cut -d ' ' -f 1 app.log | sort | uniq -c | sort -rn
2 INFO
2 ERROR
1 WARN
cut keeps the first word of each line, sort puts the same words next to each other, uniq -c collapses each run into one line with a count, and sort -rn puts the biggest count first. Each one does one small job. The same pipeline on a Mac pads the counts a little differently.
Errors don't go down a pipe or into a > file, so you can still see them. To catch them, redirect them separately:
$ ls nope
ls: cannot access 'nope': No such file or directory
$ ls nope 2> errors.txt
$ cat errors.txt
ls: cannot access 'nope': No such file or directory
$ ls app.log nope > all.txt 2>&1
$ cat all.txt
ls: cannot access 'nope': No such file or directory
app.log
2>&1 means "send errors wherever output is going". The order matters: the > all.txt has to come first, so output already points at the file when errors get pointed at output. (The Mac's ls words its error as ls: nope: No such file or directory.)
What bites. Writing back to the file you're reading. The shell sets up > before the command starts, and that empties the file, so sort finds nothing to read:
$ cp app.log copy.log
$ sort copy.log > copy.log
$ wc -l copy.log
0 copy.log
All five lines are gone. Write to a new file instead, and move it over the old one afterwards if you need to:
$ sort app.log > sorted.log
$ head -n 2 sorted.log
ERROR db down
ERROR timeout
head -n 2 prints the first two lines. tail -n 2 prints the last two.
Practise.
- Count the Python files in the project with
findandwc -l. - Save every TODO line in the project to
todos.txt, then add the line numbers of the ERROR lines inapp.logto the end of the same file. - Run
ls app.log nope 2>&1 > all.txt, with the redirects the other way round, and work out why the error appears on screen this time.
4. permissions
When you reach for it. A script says "Permission denied" when you try to run it, you've just made a file that holds a secret, or something on a server can't read a file it needs.
The idea. The ten characters at the start of ls -l are the file's type and three groups of three. The groups are the owner, the file's group, and everyone else. Each group has r (read), w (write) and x (execute), or a - where that one is off.
- rw- r-- r--
owner group others
Each letter is worth a number: r is 4, w is 2, x is 1. Add them up per group and you get the octal form chmod takes. rw- is 6, r-- is 4, r-x is 5, rwx is 7.
| octal | ls shows | typical use |
|---|---|---|
| 644 | -rw-r--r-- |
an ordinary file |
| 755 | -rwxr-xr-x |
a script or program, or a folder |
| 600 | -rw------- |
a secret only you should read |
| 700 | -rwx------ |
a private script or folder |
The commands.
$ ls -l deploy.sh
-rw-r--r-- 1 you you 14 Nov 30 01:00 deploy.sh
$ ./deploy.sh
bash: ./deploy.sh: Permission denied
$ chmod +x deploy.sh
$ ls -l deploy.sh
-rwxr-xr-x 1 you you 14 Nov 30 01:00 deploy.sh
$ ./deploy.sh
deployed
$ chmod 600 deploy.sh
$ ls -l deploy.sh
-rw------- 1 you you 14 Nov 30 01:00 deploy.sh
$ ./deploy.sh
bash: ./deploy.sh: Permission denied
+x turned execute on for all three groups and the script ran. 600 set all nine bits in one go: read and write for you, nothing for anyone else, so it can't run any more.
The symbolic form changes only the bits you name. u is the owner (user), g the group, o others and a all three. + adds, - removes and = sets exactly:
$ chmod u+x,g+r deploy.sh
$ ls -l deploy.sh
-rwxr----- 1 you you 14 Nov 30 01:00 deploy.sh
$ chmod 755 deploy.sh
$ ls -l deploy.sh
-rwxr-xr-x 1 you you 14 Nov 30 01:00 deploy.sh
Use octal when you know the whole result you want, and symbolic when you want to change one thing and leave the rest.
Folders use the same bits with different meanings. r lets you list the names inside, and x lets you go in and reach the files:
$ ls -ld src
drwxr-xr-x 2 you you 4096 Nov 30 01:00 src
$ chmod -x src
$ ls -ld src
drw-r--r-- 2 you you 4096 Nov 30 01:00 src
$ ls src
db.py main.py
$ cd src
bash: cd: src: Permission denied
$ chmod +x src
$ ls src
db.py main.py
-d makes ls -l describe the folder itself instead of what's in it. Without x, the names still list but you can't get in. The Mac's ls prints the two names and then ls: fts_read: Permission denied.
What bites. chmod 777 to make an error go away.
$ chmod 777 deploy.sh
$ ls -l deploy.sh
-rwxrwxrwx 1 you you 14 Nov 30 01:00 deploy.sh
That fixes the error by letting every user on the machine change the script, and the next person to run it runs whatever they wrote into it. Work out which bit was actually missing (usually just x on a script, or r for the user a server runs as) and add only that. The same care goes the other way for secrets: keys and .env files belong at 600, and ssh refuses to use a private key that other users can read.
Practise.
- Write the octal for
-rwxr-x---and-rw-rw-r--, then check yourself withchmodandls -lon a scratch file. - Make a script that only you can read, write and run. Do it once in octal and once in symbolic form.
- Take
roff a folder but leavexon. Can youcdinto it? Can youlsit? Can youcata file in it if you know the name?
5. processes
When you reach for it. Something takes a long time and you want your prompt back, a program has hung, or a server process needs stopping or restarting.
The idea. Every running program is a process, and each one has a number, its PID. Ending a command with & runs it in the background, so the prompt comes straight back. The shell numbers its own background jobs %1, %2 and so on. kill sends a process a signal. With no option it sends TERM, which asks the process to finish up and exit.
The commands. sleep 300 waits for five minutes, which makes it a handy process to practise on:
$ sleep 300 &
[1] 12
$ jobs
[1]+ Running sleep 300 &
$ ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
12 pts/0 00:00:00 sleep
13 pts/0 00:00:00 ps
$ pgrep -l sleep
12 sleep
$ kill 12
$ jobs
[1]+ Terminated sleep 300
$ ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
15 pts/0 00:00:00 ps
[1] 12 is the job number and the PID. jobs lists this shell's background jobs. ps lists the processes on this terminal, ps included. pgrep -l finds PIDs by name. After the kill, jobs reports how the job ended and ps no longer lists it. The shell here is PID 1 only because it ran inside a container. On your machine the numbers will be in the thousands.
On a Mac, plain ps has the same four columns but lists your processes from every terminal window you have open, not just this one. Bash 3.2 there says Terminated: 15 instead of Terminated. zsh, the Mac's default shell, prints jobs in its own format: [1] + running sleep 300, and terminated once it's killed.
You can also move a program between the foreground and the background. Start it normally, press ctrl+z to pause it, then bg lets it carry on in the background and fg brings it back to the front, where ctrl+c ends it:
$ sleep 300
^Z
[1]+ Stopped sleep 300
$ jobs
[1]+ Stopped sleep 300
$ bg
[1]+ sleep 300 &
$ jobs
[1]+ Running sleep 300 &
$ fg
sleep 300
^C
$ jobs
^Z and ^C are how the terminal shows ctrl+z and ctrl+c. A stopped job uses no CPU, but it doesn't finish either. It sits there until you bg, fg or kill it.
What bites. Going straight to kill -9.
$ sleep 300 &
[1] 17
$ kill -9 %1
$ jobs
[1]+ Killed sleep 300
-9 sends KILL, which the program never gets to see. The system just removes it, so the program can't save its work, delete its temporary files or close its connections properly. Send the plain kill first, give it a few seconds, and only use -9 if it's still there. (%1 works anywhere a PID does when the process is one of your shell's jobs. The Mac's bash says Killed: 9.)
The other one is killing the wrong thing. pgrep sleep matches every process of yours called sleep, not just the one in this terminal, and on the Mac run it found a second sleep that belonged to another program. Check the PID with ps before you kill it. A number that doesn't exist just gets an error:
$ kill 99999
bash: kill: (99999) - No such process
Practise.
- Start two
sleepcommands in the background, list them withjobs, and end the second one using its job number. - Start
sleep 300in the foreground, pause it, move it to the background, then bring it back and end it with ctrl+c. - Run
psin two terminal windows at once and compare what each one lists.
honest limitations
Five things isn't all of Linux. sudo, installing packages, ssh, editing text in the terminal, sed and awk, following a log with tail -f and managing services with systemctl all come up quickly, and none of them are here.
All the output above is from Ubuntu's GNU tools and bash 5.2. Debian, Fedora and most other distributions ship the same GNU tools. Small container images such as Alpine use BusyBox instead, which prints some of this differently. The Mac differences are noted in each section where they showed up. They're mostly wording and spacing, and the commands themselves behave the same. If you write scripts that have to run on both, test on both, because flags beyond the ones used here split between GNU and BSD more often than you'd expect.
Process ids, dates, sizes, user names and group names all depend on the machine, so yours will differ from the ones pasted here.