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 inFiles and folders
Read and search
Permissions and archives
Processes
Files and folders
pwdWhich folder you're inls -lList the folder, with permissions, owner and sizemkdir -p app/logsMake a folder (-p makes the whole path)cp notes.txt app/Copy a file (-r for a folder)mv app/notes.txt app/readme.txtMove or renamerm app/readme.txtDelete a file, no trash can (-r for a folder: careful)Read and search
cat notes.txtPrint a whole file (less for long ones)grep -n error app.logFind the lines with this word; -n gives line numbersPermissions and archives
chmod +x run.shMake a file runnable: 644 becomes 755tar -czf backup.tgz notes.txt app.logPack files into one and compress it (-x unpacks)Processes
sleep 300 && runs it in the background; the terminal stays freepsList running processes (top for a live view)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.