ข้ามไปเนื้อหาหลัก

Category: reference

Linux Terminal Tips สำหรับ Web Developer

รวม command และ pattern ที่ใช้บ่อยใน terminal — file management, git shortcuts, process, และ productivity tips สำหรับ dev workflow

· อ่านประมาณ 3 นาที

สารบัญ
cd -          # กลับไปโฟลเดอร์ล่าสุด
cd ~          # กลับ home
pushd ./src   # บันทึกตำแหน่งปัจจุบันและเปลี่ยนโฟลเดอร์
popd          # กลับไปตำแหน่งที่บันทึกไว้

# Navigate history
Ctrl+R        # ค้นหาคำสั่งที่ใช้ผ่านมา (reverse search)
!!            # คำสั่งล่าสุด
!npm          # คำสั่งล่าสุดที่ขึ้นต้นด้วย npm

File Operations

# ค้นหาไฟล์
find . -name "*.astro" -type f
find . -name "*.md" -newer package.json   # ไฟล์ .md ที่แก้หลัง package.json

# ค้นหาข้อความในไฟล์
grep -rn "transition:name" src/
grep -rn "data-pagefind" src/ --include="*.astro"

# ดูโครงสร้างโฟลเดอร์
tree -L 2 src/           # depth 2
tree -I "node_modules"   # exclude node_modules

# เปรียบเทียบไฟล์
diff file1.md file2.md
diff -u file1.md file2.md  # unified format (เหมือน git diff)

Process Management

# ดู process ที่รันอยู่
ps aux | grep node
lsof -i :4321       # ดูว่า port 4321 ถูกใช้โดยอะไร

# kill process
kill -9 <PID>
pkill -f "astro dev"   # kill ด้วยชื่อ process

# Background / Foreground
npm run dev &         # รัน background
jobs                  # ดู background jobs
fg %1                 # ดึง job 1 กลับมา foreground
Ctrl+Z                # หยุด process ชั่วคราว (ไม่ใช่ kill)

tmux Essentials

# Session management
tmux new-session -s main        # สร้าง session ใหม่
tmux attach -t main             # attach session ที่มีอยู่
tmux list-sessions              # ดู sessions ทั้งหมด
tmux kill-session -t main       # ลบ session

# ใน tmux session
Ctrl+b d          # detach (session ยังรันอยู่)
Ctrl+b c          # สร้าง window ใหม่
Ctrl+b n / p      # ไป window ถัดไป/ก่อนหน้า
Ctrl+b "          # แบ่ง pane แนวนอน
Ctrl+b %          # แบ่ง pane แนวตั้ง
Ctrl+b arrow      # เปลี่ยน pane

# ส่งคำสั่งไปยัง session อื่น
tmux send-keys -t session-name -l "npm run dev" Enter

npm / Node.js

# ดู scripts ทั้งหมด
cat package.json | grep -A 20 '"scripts"'
# หรือ
npm run

# ติดตั้ง packages เฉพาะ production
npm install --omit=dev

# ดู package version ที่ installed จริงๆ
npm list astro
npm list --depth=0   # top-level packages เท่านั้น

# ตรวจ outdated packages
npm outdated

# cache cleanup
npm cache clean --force

Git Shortcuts

# ดู log แบบ compact
git log --oneline -20
git log --oneline --graph --all   # tree view ทุก branch

# ดูการเปลี่ยนแปลงใน file
git log -p src/styles/global.css   # ดู diff ทุก commit ของไฟล์นั้น
git blame src/pages/index.astro    # ดูว่าแต่ละบรรทัดแก้โดยใคร/commit ไหน

# Undo อย่างปลอดภัย
git restore src/components/Card.astro    # discard unstaged changes
git restore --staged src/pages/about.astro  # unstage file

# Stash
git stash                  # เก็บงานที่ทำค้างไว้
git stash pop              # เอา stash กลับมา
git stash list             # ดู stash ทั้งหมด

Shell Productivity

# Aliases ที่ควรเพิ่มใน ~/.bashrc หรือ ~/.zshrc
alias gs='git status'
alias ga='git add'
alias gc='git commit -m'
alias gp='git push'
alias dev='npm run dev'
alias build='npm run build'
alias ll='ls -la'

# History แบบ timestamp
export HISTTIMEFORMAT="%F %T "

# ค้นหา command ใน history
history | grep "astro"

One-liners ที่ใช้บ่อย

# นับจำนวนไฟล์ในโฟลเดอร์
find src/content -name "*.md" | wc -l

# ดู disk usage ของโฟลเดอร์ใหญ่สุด
du -sh * | sort -hr | head -10

# Reload terminal config โดยไม่ restart
source ~/.bashrc   # หรือ source ~/.zshrc

# ดู port ที่เปิดอยู่
ss -tlnp

# สร้างหลายไฟล์พร้อมกัน
touch src/content/resources/{file1,file2,file3}.md