Setup · Terminal คือเครื่องมือ ไม่ใช่หน้าจอน่ากลัว

🖥️ Terminal พื้นฐาน — PowerShell / Terminal

Terminal = หน้าจอดำที่พิมพ์คำสั่งคุยกับเครื่อง · ไม่มี GUI · "ทำของหลายอย่างเร็วกว่า mouse" · หน้านี้สอนคำสั่งที่ใช้บ่อยที่สุด ~10 คำสั่ง ที่ครอบ 95% ของสิ่งที่ต้องใช้ในคอร์สนี้

ทำไมต้องรู้ Terminal ใน W04+ จะมีคำสั่ง python file.py · pip install pandas · git push · "ทุกอย่างพิมพ์ใน terminal" · ถ้ายังไม่รู้จัก cd/ls — หน้านี้ทำก่อน

🪟 vs 🍎 — Terminal คนละชื่อ คล้ายกัน

WindowsMac / Linux
ชื่อโปรแกรมPowerShell · Windows Terminal · CMDTerminal.app · iTerm2
เปิดยังไงWin + Rpowershell → Enter
หรือ Win + X → Terminal
Cmd + Space → "Terminal"
Shell ที่ใช้PowerShell (ใหม่) · cmd (เก่า)zsh (default ตั้งแต่ macOS Catalina) · bash
คำสั่ง lsls ใช้ได้ (alias ของ Get-ChildItem) · หรือ dirls
แยก folder ด้วยbackslash \ หรือ forward /forward /
ในคู่มือนี้ เราจะเขียนคำสั่งสำหรับ "ทั้ง 2 ระบบ" · ส่วนใหญ่เหมือนกัน · ที่ต่างจะระบุชัด ๆ

📍 10 คำสั่งที่ต้องรู้

1. pwd — ตอนนี้อยู่ folder ไหน

pwd
# Windows ตอบ: C:\Users\Ploy
# Mac ตอบ:     /Users/ploy

"Print Working Directory" · เปิด terminal ครั้งแรกอยู่ที่ home folder

2. ls หรือ dir — ดูว่าใน folder นี้มีอะไรบ้าง

ls
# หรือ Windows-only:
dir

# ดู hidden files (เริ่มต้นด้วย .)
ls -a       # Mac/PowerShell
ls -Force   # PowerShell only

3. cd — เปลี่ยน folder (Change Directory)

# เข้า folder ลูก
cd Documents

# เข้าหลาย ๆ ขั้น
cd Documents/cp/cp-w04
# หรือ Windows:
cd Documents\cp\cp-w04

# กลับขึ้นชั้น (.. = folder แม่)
cd ..

# กลับ 2 ชั้น
cd ../..

# กลับไป home folder
cd ~        # Mac/PowerShell
cd          # PowerShell — กลับ home

# ไป folder ที่ระบุ absolute path
cd C:\Users\Ploy\Documents     # Windows
cd /Users/ploy/Documents       # Mac
ลากวาง folder เข้า terminal — เร็วที่สุด Windows: ลาก folder จาก File Explorer มาวางที่ terminal → path จะถูกพิมพ์ให้อัตโนมัติ · Mac: เหมือนกัน · "ไม่ต้องพิมพ์ path ยาว ๆ เอง"

4. mkdir — สร้าง folder ใหม่

mkdir cp-w04
mkdir my-project/data       # สร้าง my-project ที่ยังไม่มีก็พังบน Windows
# PowerShell แนะ:
mkdir my-project; cd my-project; mkdir data

5. รัน Python — python file.py

# Windows
python hello.py

# Mac (Mac มี Python 2 ใน /usr/bin · ของจริงคือ python3)
python3 hello.py

# รัน Python interactive
python      # หรือ python3
>>> print("hi")
>>> exit()

6. cat หรือ type — ดูเนื้อหาไฟล์

# Mac/PowerShell
cat hello.py

# Windows CMD เก่า
type hello.py

7. cp หรือ copy — copy ไฟล์

# Mac/PowerShell
cp hello.py hello_backup.py

# Windows CMD
copy hello.py hello_backup.py

8. mv หรือ move — ย้าย / เปลี่ยนชื่อ

# Mac/PowerShell
mv hello.py hi.py            # rename
mv hello.py archive/         # ย้ายเข้า folder archive/

# Windows CMD
move hello.py hi.py
ren hello.py hi.py           # rename อย่างเดียว

9. rm หรือ del — ลบไฟล์

# Mac/PowerShell
rm hello.py
rm -r my-folder              # ลบ folder + ของข้างใน (recursive)

# Windows CMD
del hello.py
rmdir /s my-folder           # ลบ folder + ข้างใน
⚠️ ระวัง rm -r Terminal ลบไม่เข้า Recycle Bin · หายเลย · ตรวจ path ก่อนกด Enter เสมอ · "พิมพ์ ls ก่อน rm ทุกครั้ง" เพื่อยืนยันว่าจะลบของถูก

10. ล้างหน้าจอ + ออก

clear       # Mac/PowerShell
cls         # Windows CMD

# ออกจาก terminal
exit

⌨️ Shortcuts ที่ใช้ทุกวัน

Shortcutทำอะไร
/ เลื่อนดูคำสั่งที่พิมพ์ไปแล้ว — "ใช้บ่อยที่สุด"
Tabauto-complete ชื่อไฟล์ / folder · พิมพ์ cd Do + Tab → กลายเป็น cd Documents
Ctrl + Cหยุดคำสั่งที่กำลังรัน (เช่น infinite loop)
Ctrl + Lล้างหน้าจอ (เหมือน clear)
Ctrl + A / Ctrl + Ecursor ไปหัว / ท้ายบรรทัด (Mac)
Home / Endcursor ไปหัว / ท้ายบรรทัด (Windows)

🔧 Tricks ที่ AI ใช้ในตัวอย่าง — ต้องรู้

1. && — รัน 2 คำสั่งต่อเนื่อง

# รันคำสั่ง 1 ถ้าสำเร็จค่อยรันคำสั่ง 2
cd cp-w04 && python hello.py

⚠️ PowerShell เก่า (Windows 10): && ใช้ไม่ได้ · ใช้ ; แทน หรือ if ($?) { ... }

2. > — ส่ง output ไปไฟล์

python hello.py > output.txt        # เขียนใหม่ทุกครั้ง
python hello.py >> log.txt          # เพิ่มต่อท้าย

3. | (pipe) — ส่ง output ไปคำสั่งถัดไป

# Mac/Linux:
ls | grep ".py"               # หาเฉพาะไฟล์ที่ลงท้าย .py

# PowerShell:
ls | Select-String ".py"

4. wildcards

ls *.py            # ทุกไฟล์ .py
rm test_*.py       # ลบไฟล์ที่เริ่มด้วย test_
cp data*.csv backup/    # copy ทุกไฟล์ที่ขึ้นต้น data

🔧 ปัญหาที่พบบ่อย

1. 'python' is not recognized / command not found

2. cd C:\Users\พลอย\Documents ใช้ไม่ได้

3. terminal มี (base) หรือ (venv) ขึ้นต้น

4. Path มีช่องว่าง — cd My Folder พัง

# ใส่ในเครื่องหมายคำพูด
cd "My Folder"
cd "C:\Users\Ploy\My Documents"

5. PowerShell แสดงภาษาไทยเพี้ยน

chcp 65001    # เปลี่ยนเป็น UTF-8
# หรือใน $PROFILE เพิ่ม:
# [Console]::OutputEncoding = [Text.Encoding]::UTF8

6. ลืม cd เข้า folder โปรเจกต์ก่อนรัน


🎯 Workshop — ฝึก 10 นาที

  1. เปิด terminal · พิมพ์ pwd · จดที่อยู่
  2. พิมพ์ cd ~ (หรือ cd บน Windows) · ตอนนี้อยู่ home folder
  3. mkdir cp-test · cd cp-test
  4. สร้างไฟล์ hello.py ด้วย VS Code · ใส่ print("from terminal")
  5. กลับมา terminal · ls ต้องเห็น hello.py
  6. python hello.py · เห็น "from terminal" ← สำเร็จ!
  7. กด ↑ ทดสอบดูประวัติคำสั่ง
  8. cd .. ออกมา · rm -r cp-test ลบ folder ทดสอบ

🎯 Checklist ก่อนไปต่อ

ขั้นถัดไปpip + ลง package · Git + GitHub · หรือเริ่ม W01 ได้เลย