🖥️ Terminal พื้นฐาน — PowerShell / Terminal
Terminal = หน้าจอดำที่พิมพ์คำสั่งคุยกับเครื่อง · ไม่มี GUI · "ทำของหลายอย่างเร็วกว่า mouse" · หน้านี้สอนคำสั่งที่ใช้บ่อยที่สุด ~10 คำสั่ง ที่ครอบ 95% ของสิ่งที่ต้องใช้ในคอร์สนี้
ทำไมต้องรู้ Terminal
ใน W04+ จะมีคำสั่ง
python file.py · pip install pandas · git push ·
"ทุกอย่างพิมพ์ใน terminal" · ถ้ายังไม่รู้จัก cd/ls — หน้านี้ทำก่อน
🪟 vs 🍎 — Terminal คนละชื่อ คล้ายกัน
| Windows | Mac / Linux | |
|---|---|---|
| ชื่อโปรแกรม | PowerShell · Windows Terminal · CMD | Terminal.app · iTerm2 |
| เปิดยังไง | Win + R → powershell → Enterหรือ Win + X → Terminal | Cmd + Space → "Terminal" |
| Shell ที่ใช้ | PowerShell (ใหม่) · cmd (เก่า) | zsh (default ตั้งแต่ macOS Catalina) · bash |
| คำสั่ง ls | ls ใช้ได้ (alias ของ Get-ChildItem) · หรือ dir | ls |
| แยก 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 | ทำอะไร |
|---|---|
| ↑ / ↓ | เลื่อนดูคำสั่งที่พิมพ์ไปแล้ว — "ใช้บ่อยที่สุด" |
| Tab | auto-complete ชื่อไฟล์ / folder · พิมพ์ cd Do + Tab → กลายเป็น cd Documents |
| Ctrl + C | หยุดคำสั่งที่กำลังรัน (เช่น infinite loop) |
| Ctrl + L | ล้างหน้าจอ (เหมือน clear) |
| Ctrl + A / Ctrl + E | cursor ไปหัว / ท้ายบรรทัด (Mac) |
| Home / End | cursor ไปหัว / ท้ายบรรทัด (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
- Python ยังไม่ลง หรือลืม Add to PATH · กลับไปดู setup-python
- Mac — ลอง
python3แทนpython
2. cd C:\Users\พลอย\Documents ใช้ไม่ได้
- ภาษาไทยใน path ทำให้บางคำสั่งพัง — ใช้ folder ภาษาอังกฤษเสมอ
- Windows: เปลี่ยนชื่อ Windows user เป็นอังกฤษ (ลำบาก) · หรือสร้าง folder ที่
C:\dev\cp\ใช้แทน
3. terminal มี (base) หรือ (venv) ขึ้นต้น
(base)= อยู่ใน Anaconda env(venv)= อยู่ใน Python virtual environment- ดีไม่ใช่เลว · บอกว่าใช้ Python ตัวไหน
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 โปรเจกต์ก่อนรัน
- เปิด terminal มา อยู่ที่ home ·
python hello.py→ "No such file" - ต้อง
cdเข้า folder ที่มีไฟล์ก่อน - เร็วกว่า: เปิด terminal จาก VS Code (Ctrl + `) → terminal อยู่ที่ folder โปรเจกต์อัตโนมัติ
🎯 Workshop — ฝึก 10 นาที
- เปิด terminal · พิมพ์
pwd· จดที่อยู่ - พิมพ์
cd ~(หรือcdบน Windows) · ตอนนี้อยู่ home folder mkdir cp-test·cd cp-test- สร้างไฟล์
hello.pyด้วย VS Code · ใส่print("from terminal") - กลับมา terminal ·
lsต้องเห็นhello.py python hello.py· เห็น "from terminal" ← สำเร็จ!- กด ↑ ทดสอบดูประวัติคำสั่ง
cd ..ออกมา ·rm -r cp-testลบ folder ทดสอบ
🎯 Checklist ก่อนไปต่อ
- ☐ เปิด terminal ได้ (PowerShell หรือ Terminal.app)
- ☐
pwd·ls·cd folder·cd ..ใช้ได้ - ☐ สร้าง folder ใหม่ด้วย
mkdir - ☐
python file.pyรันได้ - ☐ ใช้ ↑ ดูประวัติคำสั่ง · Tab auto-complete