Linux cheatsheet

13 commands, run one after another in /home/mai. Files on the left, processes on the right, the terminal below them. The sheet plays them in order; pick one to watch it again.

pwdWhich folder you're in
/home/maiapp.lognotes.txtrun.sh644processes1 sh$ pwd/home/mai

Files and folders

Read and search

Permissions and archives

Processes

Files and folders

/home/maiapp.lognotes.txtrun.sh644processes1 sh$ pwd/home/mai
pwdWhich folder you're in
/home/maiapp.lognotes.txtrun.sh644processes1 sh$ ls -l-rw-r--r-- app.log-rw-r--r-- notes.txt-rw-r--r-- run.sh
ls -lList the folder, with permissions, owner and size
/home/maiapp.logapp/logs/notes.txtrun.sh644processes1 sh$ mkdir -p app/logs
mkdir -p app/logsMake a folder (-p makes the whole path)
/home/maiapp.logapp/logs/notes.txtnotes.txtrun.sh644processes1 sh$ cp notes.txt app/
cp notes.txt app/Copy a file (-r for a folder)
/home/maiapp.logapp/logs/notes.txtreadme.txtnotes.txtrun.sh644processes1 sh$ mv app/notes.txt ap…
mv app/notes.txt app/readme.txtMove or rename
/home/maiapp.logapp/logs/readme.txtnotes.txtrun.sh644processes1 sh$ rm app/readme.txt
rm app/readme.txtDelete a file, no trash can (-r for a folder: careful)

Read and search

/home/maiapp.logapp/logs/notes.txtrun.sh644processes1 sh$ cat notes.txtbuy milk
cat notes.txtPrint a whole file (less for long ones)
/home/maiapp.logapp/logs/notes.txtrun.sh644processes1 sh$ grep -n error app.l…2:error: disk full
grep -n error app.logFind the lines with this word; -n gives line numbers

Permissions and archives

/home/maiapp.logapp/logs/notes.txtrun.sh755processes1 sh$ chmod +x run.sh
chmod +x run.shMake a file runnable: 644 becomes 755
/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755processes1 sh$ tar -czf backup.tgz…notes.txtapp.log
tar -czf backup.tgz notes.txt app.logPack files into one and compress it (-x unpacks)

Processes

/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755processes1 sh2 sleep 300$ sleep 300 &
sleep 300 && runs it in the background; the terminal stays free
/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755processes1 sh2 sleep 300$ ps1
psList running processes (top for a live view)
/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755processes1 sh$ kill $!
kill $!Signal a process to stop, by PID ($! is the last one started in the background)

What the numbers mean

644 and 755

Three digits: the owner, the group, everyone else. 4 is read, 2 is write, 1 is run, added up: 6 = read + write, 5 = read + run, 7 = all three. chmod +x adds run for everyone.

Checked

linux.sh
#!/bin/sh
# Every command on the Linux cheatsheet, run for real inside an alpine:3.20 container (already here, nothing pulled;
# removed when it ends): one after another in a home folder, and after each a line saying what is there: the files,
# run.sh's mode, the sleeping job, and what the command printed. The page's pictures are held to these lines
# (lib/cheat-linux.test.ts). Run: sh linux.sh   (needs docker)
set -eu
docker run --rm -i --network none alpine:3.20 sh -s <<'INSIDE'
set -eu
mkdir -p /home/mai && cd /home/mai
printf 'buy milk\n' > notes.txt
printf 'ok\nerror: disk full\nok\n' > app.log
printf '#!/bin/sh\necho hi\n' > run.sh
chmod 644 run.sh
say() {
  files=$(find . -mindepth 1 | sed 's|^\./||' | while read -r f; do if [ -d "$f" ]; then echo "$f/"; else echo "$f"; fi; done | sort | tr '\n' ' ' | sed 's/ $//')
  jobs=$(ps -o args | grep -c '^sleep 300' || true)
  echo "$1: files=[$files] run.sh=$(stat -c %a run.sh) sleeping=$jobs out=[$2]"
}
say pwd "$(pwd)"
say ls "$(ls -l | sed '/^total/d' | awk '{ print $1 " " $NF }' | tr '\n' ',' | sed 's/,$//')"
mkdir -p app/logs; say mkdir ""
cp notes.txt app/; say cp ""
mv app/notes.txt app/readme.txt; say mv ""
rm app/readme.txt; say rm ""
say cat "$(cat notes.txt)"
say grep "$(grep -n error app.log)"
chmod +x run.sh; say chmod ""
tar -czf backup.tgz notes.txt app.log; say tar "$(tar -tzf backup.tgz | tr '\n' ',' | sed 's/,$//')"
sleep 300 &
say sleep ""
say ps "$(ps -o args | grep -c '^sleep 300')"
kill $!; wait $! 2>/dev/null || true; say kill ""
INSIDE
Run: sh linux.sh (needs Docker)It printed:
pwd: files=[app.log notes.txt run.sh] run.sh=644 sleeping=0 out=[/home/mai]
ls: files=[app.log notes.txt run.sh] run.sh=644 sleeping=0 out=[-rw-r--r-- app.log,-rw-r--r-- notes.txt,-rw-r--r-- run.sh]
mkdir: files=[app.log app/ app/logs/ notes.txt run.sh] run.sh=644 sleeping=0 out=[]
cp: files=[app.log app/ app/logs/ app/notes.txt notes.txt run.sh] run.sh=644 sleeping=0 out=[]
mv: files=[app.log app/ app/logs/ app/readme.txt notes.txt run.sh] run.sh=644 sleeping=0 out=[]
rm: files=[app.log app/ app/logs/ notes.txt run.sh] run.sh=644 sleeping=0 out=[]
cat: files=[app.log app/ app/logs/ notes.txt run.sh] run.sh=644 sleeping=0 out=[buy milk]
grep: files=[app.log app/ app/logs/ notes.txt run.sh] run.sh=644 sleeping=0 out=[2:error: disk full]
chmod: files=[app.log app/ app/logs/ notes.txt run.sh] run.sh=755 sleeping=0 out=[]
tar: files=[app.log app/ app/logs/ backup.tgz notes.txt run.sh] run.sh=755 sleeping=0 out=[notes.txt,app.log]
sleep: files=[app.log app/ app/logs/ backup.tgz notes.txt run.sh] run.sh=755 sleeping=1 out=[]
ps: files=[app.log app/ app/logs/ backup.tgz notes.txt run.sh] run.sh=755 sleeping=1 out=[1]
kill: files=[app.log app/ app/logs/ backup.tgz notes.txt run.sh] run.sh=755 sleeping=0 out=[]

The same commands, one after another, in an alpine:3.20 container with no network (already here, nothing pulled; removed when it ends). After each, the files, run.sh's mode, the sleeping job and what it printed; the site's tests compare the pictures with these lines.