กลับไปหน้าสูตร
#shell#terminal#command#cheatsheet

Shell Command Cheatsheet

รวมคำสั่ง Shell แบบจัดเต็ม: navigation, files, text processing, process, network, permissions, archive, ssh และ productivity

11 มีนาคม 2569อ่านประมาณ 3 นาที

File & Directory

Commandใช้ทำอะไร
mkdir -p app/srcสร้างโฟลเดอร์ซ้อน
touch app.logสร้างไฟล์เปล่า
cp file1 file2copy ไฟล์
cp -r src backupcopy โฟลเดอร์
mv old newrename/move
rm file.txtลบไฟล์
rm -rf buildลบโฟลเดอร์แบบ force

Text & File Reading

Commandใช้ทำอะไร
cat file.txtแสดงไฟล์ทั้งหมด
less file.txtเปิดอ่านแบบ scroll
head -n 30 file.txtดู 30 บรรทัดแรก
tail -n 30 file.txtดู 30 บรรทัดท้าย
tail -f app.logดู log แบบ realtime
wc -l file.txtนับบรรทัด

Search & Filter

Commandใช้ทำอะไร
find . -name "*.ts"หาไฟล์ตามชื่อ
grep -R "TODO" .ค้นหา text recursive
rg "TODO"ค้นหาเร็วด้วย ripgrep
rg --filesแสดงไฟล์ทั้งหมด
sort file.txtเรียงบรรทัด
uniq file.txtตัดบรรทัดซ้ำ (ต้อง sort ก่อนในหลายกรณี)

Pipes & Redirection

Patternตัวอย่าง
pipe`cat app.log
redirect outputnpm test > test.log
append outputecho "done" >> deploy.log
redirect stderrcmd 2> error.log
stdout+stderrcmd > all.log 2>&1

Process Management

Commandใช้ทำอะไร
ps auxดู process ทั้งหมด
top / htopmonitor process
pgrep -f "node"หา PID ตามชื่อ
kill <pid>kill ปกติ
kill -9 <pid>force kill
pkill -f "next dev"kill ตาม pattern
lsof -i :3000process ที่จับ port 3000

Network & HTTP

Commandใช้ทำอะไร
curl -I https://example.comดู headers
curl -L https://example.comfollow redirects
curl -X POST ...ยิง API
ping google.comทดสอบ network
nslookup example.comquery DNS
traceroute example.comtrace route
netstat -an / ss -tulpenดู sockets

Permissions & Ownership

Commandใช้ทำอะไร
chmod +x script.shทำไฟล์ให้ execute ได้
chmod 644 file.txtset permission
chmod 755 dirset permission dir
chown user:group fileเปลี่ยนเจ้าของไฟล์
umaskดู default mask

Archive & Compression

Commandใช้ทำอะไร
tar -czf backup.tar.gz project/บีบอัดเป็น tar.gz
tar -xzf backup.tar.gzแตก tar.gz
zip -r backup.zip project/zip โฟลเดอร์
unzip backup.zipแตก zip
gzip file.logบีบอัดไฟล์
gunzip file.log.gzแตก gzip

SSH & Remote

Commandใช้ทำอะไร
ssh user@hostSSH เข้าเครื่องปลายทาง
ssh -i ~/.ssh/key.pem user@hostSSH ด้วย key
scp file user@host:/path/copy file ขึ้น server
scp -r dir user@host:/path/copy directory
rsync -avz src/ user@host:/dst/sync ไฟล์ประสิทธิภาพสูง

Environment & Variables

Commandใช้ทำอะไร
echo $PATHดู PATH
export NODE_ENV=productionตั้ง env ชั่วคราว
envดู env ทั้งหมด
`printenvrg NODE`
source ~/.zshrcreload shell config

Productivity Shortcuts (Shell)

Keyใช้ทำอะไร
Ctrl + Aไปต้นบรรทัด
Ctrl + Eไปท้ายบรรทัด
Ctrl + Uลบจาก cursor ไปต้นบรรทัด
Ctrl + Kลบจาก cursor ไปท้ายบรรทัด
Ctrl + Wลบคำก่อนหน้า
Ctrl + Rค้นหาคำสั่งย้อนหลัง
Ctrl + Cยกเลิกคำสั่งปัจจุบัน
Ctrl + Lล้างจอ

Useful Aliases

alias ll='ls -la'
alias gs='git status -sb'
alias ga='git add .'
alias gc='git commit -m'
alias gp='git push'
alias ..='cd ..'
alias ...='cd ../..'

Practical One-liners

# หาไฟล์ใหญ่สุด 20 ไฟล์ในโฟลเดอร์ปัจจุบัน
find . -type f -print0 | xargs -0 du -h | sort -hr | head -n 20

# หา process ที่กินพอร์ต 3000 แล้ว kill
lsof -ti :3000 | xargs kill -9

# ดู error log แบบ realtime เฉพาะคำว่า ERROR
tail -f app.log | rg ERROR

# แทนที่ข้อความทุกไฟล์ .env.example
rg -l 'localhost' . | xargs sed -i '' 's/localhost/127.0.0.1/g'

Daily Workflow Template

StepCommand
ตรวจ path/projpwd && ls -la
sync ล่าสุดgit pull --rebase
run appnpm run dev
debug logstail -f logs/app.log
cleanuprm -rf .cache tmp