Linux cheatsheet

13 คำสั่ง รันต่อกันใน /home/mai ไฟล์อยู่ซ้าย process อยู่ขวา terminal อยู่ข้างล่าง ชีตจะเล่นตามลำดับ กดตัวไหนก็ได้เพื่อดูซ้ำ

pwdตอนนี้อยู่โฟลเดอร์ไหน
/home/maiapp.lognotes.txtrun.sh644process1 sh$ pwd/home/mai

ไฟล์กับโฟลเดอร์

อ่านและค้น

สิทธิ์และไฟล์บีบอัด

process

ไฟล์กับโฟลเดอร์

/home/maiapp.lognotes.txtrun.sh644process1 sh$ pwd/home/mai
pwdตอนนี้อยู่โฟลเดอร์ไหน
/home/maiapp.lognotes.txtrun.sh644process1 sh$ ls -l-rw-r--r-- app.log-rw-r--r-- notes.txt-rw-r--r-- run.sh
ls -lดูของในโฟลเดอร์ พร้อมสิทธิ์ เจ้าของ ขนาด
/home/maiapp.logapp/logs/notes.txtrun.sh644process1 sh$ mkdir -p app/logs
mkdir -p app/logsสร้างโฟลเดอร์ (-p สร้างทั้งทางที่ยังไม่มี)
/home/maiapp.logapp/logs/notes.txtnotes.txtrun.sh644process1 sh$ cp notes.txt app/
cp notes.txt app/ก๊อปไฟล์ (-r สำหรับโฟลเดอร์)
/home/maiapp.logapp/logs/notes.txtreadme.txtnotes.txtrun.sh644process1 sh$ mv app/notes.txt ap…
mv app/notes.txt app/readme.txtย้ายหรือเปลี่ยนชื่อ
/home/maiapp.logapp/logs/readme.txtnotes.txtrun.sh644process1 sh$ rm app/readme.txt
rm app/readme.txtลบไฟล์ ไม่มีถังขยะ (-r ลบทั้งโฟลเดอร์ ระวัง)

อ่านและค้น

/home/maiapp.logapp/logs/notes.txtrun.sh644process1 sh$ cat notes.txtbuy milk
cat notes.txtพิมพ์ไฟล์ทั้งไฟล์ (ไฟล์ยาวใช้ less)
/home/maiapp.logapp/logs/notes.txtrun.sh644process1 sh$ grep -n error app.l…2:error: disk full
grep -n error app.logหาบรรทัดที่มีคำนี้ -n บอกเลขบรรทัด

สิทธิ์และไฟล์บีบอัด

/home/maiapp.logapp/logs/notes.txtrun.sh755process1 sh$ chmod +x run.sh
chmod +x run.shให้ไฟล์รันได้: 644 กลายเป็น 755
/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755process1 sh$ tar -czf backup.tgz…notes.txtapp.log
tar -czf backup.tgz notes.txt app.logแพ็กหลายไฟล์เป็นไฟล์เดียวแล้วบีบ (-x แตกออก)

process

/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755process1 sh2 sleep 300$ sleep 300 &
sleep 300 && รันเบื้องหลัง terminal ว่างให้พิมพ์ต่อ
/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755process1 sh2 sleep 300$ ps1
psดู process ที่รันอยู่ (top ดูแบบสด)
/home/maiapp.logapp/logs/backup.tgznotes.txtrun.sh755process1 sh$ kill $!
kill $!ส่งสัญญาณให้ process หยุดตาม PID ($! คือตัวล่าสุดที่รันเบื้องหลัง)

ตัวเลขพวกนั้นคืออะไร

644 กับ 755

สามหลักคือ เจ้าของ กลุ่ม และคนอื่นทั้งหมด 4 = อ่าน 2 = เขียน 1 = รัน เอามาบวกกัน 6 = อ่าน + เขียน 5 = อ่าน + รัน 7 = ได้หมด chmod +x เติมสิทธิ์รันให้ทุกคน

ตรวจแล้ว

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
รัน: sh linux.sh (ต้องมี Docker)ผลที่พิมพ์ออกมา:
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=[]

คำสั่งชุดเดียวกัน รันต่อกันใน container alpine:3.20 ที่ไม่มี network (มีอยู่แล้ว ไม่ดึงใหม่ ลบทิ้งตอนจบ) หลังแต่ละคำสั่งพิมพ์ไฟล์ที่มี สิทธิ์ของ run.sh งานที่รันเบื้องหลัง และสิ่งที่คำสั่งพิมพ์ออกมา test ของเว็บเทียบภาพกับบรรทัดพวกนี้