terminal 102: files without fear terminal 102: จัดการไฟล์โดยไม่ต้องกลัว
create, read, copy, move, rename, and delete files from the terminal — with the most important safety rule: `rm` has no Trash สร้าง อ่าน คัดลอก ย้าย เปลี่ยนชื่อ และลบไฟล์จาก terminal พร้อมกฎสำคัญที่สุดที่ต้องรู้: `rm` ไม่มีถังขยะ
in terminal 101 you learned to look around — pwd, ls, cd. that was the safe part.
now you’re going to change things: create files, copy them, rename them, delete them. the commands are short and straightforward. one of them (rm) skips the Trash entirely, so this post is really about learning the commands and building one good habit around them.
learn five core commands, one important variation, and one safety habit. after that, files stop feeling mysterious.
what you’ll know by the end
- how to create an empty file (
touch) and read a text file (cat/type) - how to copy (
cp), move (mv), and rename files — rename is just move-in-place - how to delete files safely (
rm) and delete a whole folder (rm -r) - how to see hidden files (
ls -a) and handle file names with spaces ("quotes") - how to act on many files at once with
*(a wildcard pattern) - the one rule that prevents most mistakes: pause before
rm
what this assumes
just terminal 101. if pwd, ls, and cd feel natural, you’re ready.
5 core commands + 1 important variation
the simulator below keeps most file-management commands the same on Mac and Windows so you can focus on the concepts first. on a real Windows machine, you may also run into other command names depending on the shell you’re using.
| what you want to do | Mac | Windows in this lesson | mnemonic |
|---|---|---|---|
| create an empty file | touch notes.md | touch notes.md | ”touch” it into existence |
| read a text file | cat notes.md | type notes.md | ”concatenate” / print out |
| copy a file | cp notes.md notes.bak | cp notes.md notes.bak | copy |
| move or rename a file | mv notes.bak archive.md | mv notes.bak archive.md | move |
| delete a file | rm notes.md | rm notes.md | remove |
| delete a folder and contents | rm -r folder | rm -r folder | recursive remove |
rename is just move. mv old.txt new.txt renames. mv notes.txt ~/Desktop/ moves. same command, different destination.
real Windows note: the simulator accepts
touch,cp,mv,rmso you can focus on the concepts. on real PowerShell,touchis not built in — useNew-Item notes.mdinstead.cp,mv, andrmexist on PowerShell as aliases for the real cmdlets, so they work as shown.
try it yourself
here’s a terminal you can practice in. it’s fake, so nothing can hurt your real computer, and it’s close enough to a real shell to practice the basics safely.
walk through this sequence:
pwd— confirm where you arels(Mac) ordir(Windows) — see what’s herecd Desktop— step into Desktoptouch todo.md— create an empty filels(Mac) ordir(Windows) — your new file is therecat notes.txt(Mac) ortype notes.txt(Windows) — read an existing filecp notes.txt notes.bak— copy itmv notes.bak old-notes.txt— rename the copyrm old-notes.txt— delete the copyls(Mac) ordir(Windows) — confirm it’s gone
no dialog. no Trash. if you delete it, it’s gone. that’s why rm deserves extra care.
ใน terminal 101 เราฝึกแค่ “มองรอบๆ” ด้วย pwd, ls, cd ซึ่งเป็นส่วนที่ปลอดภัยครับ
ในโพสต์นี้เราจะ ลงมือเปลี่ยนอะไรจริงๆ แล้ว
สร้างไฟล์ คัดลอก เปลี่ยนชื่อ ลบ
คำสั่งไม่ยากเลยครับ แต่มีตัวหนึ่ง (rm) ที่ลบแบบไม่ผ่านถังขยะ
ที่พูดตั้งแต่ต้นไม่ใช่เพื่อให้กลัว แต่เพื่อให้จำเรื่องสำคัญที่สุดของโพสต์นี้ได้ตั้งแต่แรก
เรียน 5 คำสั่งหลัก รู้เพิ่มอีก 1 รูปแบบ และสร้างนิสัยที่ถูกต้อง 1 อย่าง แล้วไฟล์ก็จะไม่น่ากลัวอีกต่อไป
จบโพสต์นี้แล้วจะทำอะไรได้บ้าง
- สร้างไฟล์เปล่า (
touch) และอ่านไฟล์ข้อความ (cat/type) - คัดลอก (
cp), ย้าย (mv), เปลี่ยนชื่อไฟล์ — การเปลี่ยนชื่อก็คือการย้ายในที่เดิมนั่นแหละครับ - ลบไฟล์อย่างปลอดภัย (
rm) และลบโฟลเดอร์ทั้งอันพร้อมของข้างใน (rm -r) - เห็นไฟล์ที่ซ่อนอยู่ (
ls -a) และจัดการชื่อไฟล์ที่มีเว้นวรรค ("ใส่เครื่องหมายคำพูด") - ใช้
*เป็นสัญลักษณ์แทนหลายไฟล์พร้อมกัน - กฎข้อเดียวที่ช่วยลดความพลาดส่วนใหญ่ได้: หยุดดูคำสั่งก่อนกด Enter หลัง
rm
โพสต์นี้เริ่มจากไหน
แค่ terminal 101 ก็พอครับ
ถ้า pwd, ls, cd รู้สึกคล่องมือแล้ว ก็ไปต่อได้เลย
5 คำสั่งหลัก + 1 รูปแบบที่ต้องรู้
เพื่อให้โฟกัสที่แนวคิดก่อน ตัวจำลองด้านล่างจะทำให้คำสั่งจัดการไฟล์ส่วนใหญ่ใช้เหมือนกันทั้ง Mac และ Windows แต่บน Windows เครื่องจริง คุณอาจเจอชื่อคำสั่งอื่นได้ ขึ้นอยู่กับโปรแกรมที่ใช้พิมพ์คำสั่ง
| สิ่งที่อยากทำ | Mac | Windows ในบทนี้ | วิธีจำ |
|---|---|---|---|
| สร้างไฟล์เปล่า | touch notes.md | touch notes.md | ”แตะ” ให้เกิดขึ้นมา |
| อ่านไฟล์ข้อความ | cat notes.md | type notes.md | แสดงเนื้อหาออกมา |
| คัดลอกไฟล์ | cp notes.md notes.bak | cp notes.md notes.bak | copy |
| ย้ายหรือเปลี่ยนชื่อไฟล์ | mv notes.bak archive.md | mv notes.bak archive.md | move |
| ลบไฟล์ | rm notes.md | rm notes.md | remove |
| ลบโฟลเดอร์ทั้งอัน | rm -r folder | rm -r folder | ลบทั้งโฟลเดอร์และสิ่งที่อยู่ข้างใน |
เปลี่ยนชื่อ = ย้าย ครับ
mv old.txt new.txt คือเปลี่ยนชื่อ
mv notes.txt ~/Desktop/ คือย้าย
คำสั่งเดียวกัน ต่างกันที่ปลายทาง
หมายเหตุสำหรับ Windows จริง: ใน simulator นี้
touch,cp,mv,rmใช้ได้หมดเพื่อให้โฟกัสที่แนวคิดก่อนครับ แต่บน PowerShell จริงๆtouchไม่ได้ติดตั้งมาในตัว ให้ใช้New-Item notes.mdแทน ส่วนcp,mv,rmมีให้ใช้เป็น alias ของ cmdlet จริงอยู่แล้ว จึงใช้งานได้เหมือนในบทเรียน
ลองเล่นดู
terminal ข้างล่างเป็นแบบจำลองครับ — ไม่กระทบเครื่องจริง และใกล้ของจริงพอสำหรับฝึกพื้นฐานได้อย่างปลอดภัย
ลองทำตามนี้ทีละขั้น:
pwd— ยืนยันว่าอยู่ที่ไหนls(Mac) หรือdir(Windows) — ดูว่ามีอะไรอยู่cd Desktop— เข้าไปใน Desktoptouch todo.md— สร้างไฟล์เปล่าขึ้นมาls(Mac) หรือdir(Windows) — เห็นไฟล์ใหม่แล้วcat notes.txt(Mac) หรือtype notes.txt(Windows) — อ่านไฟล์ที่มีอยู่cp notes.txt notes.bak— คัดลอกmv notes.bak old-notes.txt— เปลี่ยนชื่อสำเนาrm old-notes.txt— ลบสำเนาls(Mac) หรือdir(Windows) — ยืนยันว่ามันหายไปแล้วจริง
ไม่มีหน้าต่างถาม ไม่ไปถังขยะ ถ้าลบแล้วก็คือลบเลย นี่แหละครับคือสิ่งที่ต้องระวังเวลาใช้ rm
the real-world task
this one’s small, but it’s a workflow you’ll use constantly. do it in the terminal above:
cd ~— head hometouch README.md— create itcp README.md README.bak— make a backupmv README.bak README.old— rename the backupls(Mac) ordir(Windows) — both files are thererm README.old— delete the backupls(Mac) ordir(Windows) — onlyREADME.mdremains
create → copy → rename → delete. almost every project includes some version of this.
hidden files and the . prefix
files and folders starting with a dot (.gitignore, .env, .zshrc) are hidden by default. ls won’t show them. you’ll meet plenty of them soon — every project has a .gitignore, every shell config is a dotfile.
see them all with -a:
ls -a # Mac and Windows (both supported here)try cd ~ then ls -a in the simulator. the .zshrc file appears only when you ask to show hidden files.
they’re not secret. the dot is just a convention that says “config file — don’t clutter the normal listing.”
names with spaces
if a file or folder has a space in its name, the terminal treats each word as a separate piece of the command. solution: wrap it in quotes.
cd "My Documents"
cat "weekly notes.md"easier option: avoid spaces in names you create yourself. use - or _ instead. weekly-notes.md will save you trouble for the next decade.
acting on many files with *
* means “anything.” this kind of pattern is often called a glob.
ls *.pdf # list every PDF
ls *.txt *.jpg # list every txt and every jpg
rm *.bak # delete every backup file (careful!)try cd Desktop then ls *.pdf (Mac) or dir *.pdf (Windows) in the terminal above. the shell expands *.pdf into every matching file before running the command.
tip: before running
rm *.something, swap thermforlsfirst. iflsshows the right list, then runrm. this is a simple habit that prevents a lot of avoidable mistakes.
why rm matters more than the others
rm does not use the Trash. there is no confirmation dialog. if you delete the wrong file, your only easy way back is a backup or cloud sync that was already set up beforehand.
there are only two rules you need:
- read the command before pressing Enter. especially when there’s a
*in it. especially when there’s a-rin it. - never run
rm -rf /(orrm -rf ~, or anyrm -rfon a path you’re not 100% sure about). that command tries to delete everything it can reach.
that’s it. follow those two rules and rm stays manageable.
common mistakes
“No such file or directory” on cp or mv. you’re probably not in the folder you think you’re in. pwd first. or give the full path.
rm fails on a folder. plain rm won’t delete a folder — you need rm -r folder (recursive). the simulator above will tell you “is a directory” if you forget the -r.
mv silently overwrites. if new.txt already exists and you run mv old.txt new.txt, the old new.txt is replaced. no warning. if that matters, ls first to check.
you delete the wrong file with a wildcard. you meant rm *.bak but typed rm * (which deletes everything in the folder). always ls the pattern before using it with rm.
what this unlocks
you can now make, copy, rename, and delete files without the help of a file manager. that’s already enough for most day-to-day terminal work.
next up: terminal 103 — pipes, redirects, and finding things. that’s the point where single commands stop being the whole story. you’ll send one command’s output into another, save results to files, and search through a project without clicking folder by folder.
โจทย์จากโลกจริง
อันนี้เล็กๆ ครับ แต่เป็นจังหวะการทำงานที่คุณจะได้ใช้บ่อยมาก ลองทำใน terminal ด้านบนได้เลย:
cd ~— กลับ hometouch README.md— สร้างไฟล์cp README.md README.bak— ทำสำเนาไว้เป็นไฟล์สำรองmv README.bak README.old— เปลี่ยนชื่อไฟล์สำรองls(Mac) หรือdir(Windows) — เห็นทั้งสองไฟล์rm README.old— ลบไฟล์สำรองls(Mac) หรือdir(Windows) — เหลือREADME.mdอันเดียว
สร้าง → คัดลอก → เปลี่ยนชื่อ → ลบ แทบทุกโปรเจ็กต์ล้วนมีจังหวะแบบนี้อยู่ในรูปใดรูปหนึ่งครับ
ไฟล์ที่ซ่อนและจุด (.)
ไฟล์หรือโฟลเดอร์ที่ขึ้นต้นด้วยจุด (.gitignore, .env, .zshrc) จะ ถูกซ่อนโดยค่าเริ่มต้น
ls ปกติจะไม่แสดงให้เห็นครับ
แต่คุณจะเจอมันบ่อยมาก ทุกโปรเจ็กต์มักมี .gitignore และไฟล์ตั้งค่าของ shell ก็เป็นไฟล์กลุ่มนี้ทั้งนั้น
ถ้าอยากเห็นทั้งหมด ใส่ -a:
ls -a # ใช้ได้ทั้ง Mac และ Windows ในตัวจำลองนี้ลอง cd ~ แล้วตามด้วย ls -a ใน simulator ครับ ไฟล์ .zshrc จะโผล่มาเฉพาะตอนที่เราขอดูไฟล์ที่ซ่อนอยู่
ไฟล์พวกนี้ไม่ได้ลับอะไรครับ จุดหน้าชื่อเป็นแค่ธรรมเนียมว่า “นี่คือไฟล์ตั้งค่า ไม่ต้องมาเกะกะใน ls ปกติ”
ชื่อไฟล์ที่มีเว้นวรรค
ถ้าไฟล์หรือโฟลเดอร์มีเว้นวรรคในชื่อ terminal จะคิดว่าแต่ละคำเป็นคนละส่วนของคำสั่ง ทางแก้: ครอบด้วยเครื่องหมายคำพูด
cd "My Documents"
cat "weekly notes.md"ทางเลือกที่ง่ายกว่า: ถ้าคุณตั้งชื่อเอง เลี่ยงการใส่เว้นวรรคไปเลย
ใช้ - หรือ _ แทน เช่น weekly-notes.md จะสบายไปอีกสิบปีครับ
สั่งงานหลายไฟล์พร้อมกันด้วย *
* แปลว่า “อะไรก็ได้” เป็นสัญลักษณ์แทนชื่อไฟล์หลายแบบ ซึ่งมักเรียกว่า glob
ls *.pdf # ดูไฟล์ PDF ทั้งหมด
ls *.txt *.jpg # ดูไฟล์ txt และ jpg ทั้งหมด
rm *.bak # ลบไฟล์สำรองทั้งหมด (ระวัง!)ลองใน terminal ด้านบน: cd Desktop แล้วใช้ ls *.pdf (Mac) หรือ dir *.pdf (Windows) ดูครับ
ตัวโปรแกรมจะขยาย *.pdf ให้เป็นชื่อไฟล์จริงทุกตัวก่อนรันคำสั่ง
เคล็ดลับ: ก่อนจะ
rm *.อะไรก็ตามให้ใช้lsดูก่อนด้วยรูปแบบเดียวกัน ถ้าlsขึ้นมาแล้วตรงกับที่ต้องการ ค่อยrmนิสัยนี้ช่วยกันความพลาดที่ไม่จำเป็นได้เยอะมากครับ
ทำไม rm ถึงต้องระวังเป็นพิเศษ
rm ไม่ส่งไฟล์ไปถังขยะ และไม่มีกล่องยืนยัน
ถ้าลบผิด ทางกลับที่ง่ายที่สุดก็คือระบบสำรองข้อมูลหรือการซิงก์ผ่านคลาวด์ที่คุณ ตั้งไว้ก่อนหน้า เท่านั้นครับ
กฎที่ต้องจำมีแค่ 2 ข้อ:
- อ่านคำสั่งอีกครั้งก่อนกด Enter โดยเฉพาะเมื่อมี
*หรือมี-rอยู่ในนั้น - ห้ามรัน
rm -rf /(หรือrm -rf ~หรือrm -rfบน path ที่คุณไม่แน่ใจ 100%) เพราะมันพยายามลบทุกอย่างที่มันเข้าถึงได้
แค่นั้นครับ
ทำตาม 2 ข้อนี้ rm ก็จะเป็นแค่เครื่องมือที่ใช้อย่างมีสติได้
ข้อผิดพลาดที่พบบ่อย
“No such file or directory” ตอน cp หรือ mv ส่วนใหญ่คืออยู่ผิดโฟลเดอร์ครับ ลอง pwd ก่อน หรือให้ path เต็มไปเลย
rm ไม่ยอมลบโฟลเดอร์ rm ธรรมดาลบได้แต่ไฟล์ ถ้าเป็นโฟลเดอร์ต้องใช้ rm -r folder (recursive)
simulator ด้านบนจะบอก “is a directory” ถ้าลืม -r
mv ทับไฟล์เงียบๆ ถ้ามี new.txt อยู่แล้ว แล้วคุณรัน mv old.txt new.txt ตัว new.txt เดิมจะถูกทับทันทีโดยไม่เตือน
ถ้าสำคัญ ให้ ls เช็คก่อน
ลบผิดไฟล์เพราะ * อยากพิมพ์ rm *.bak แต่พิมพ์ rm * (ลบทุกอย่างในโฟลเดอร์) — ls ดูรูปแบบก่อนใช้กับ rm ทุกครั้งครับ
เปิดประตูไปต่อที่ไหน
ตอนนี้คุณสร้าง คัดลอก เปลี่ยนชื่อ และลบไฟล์ได้โดยไม่ต้องพึ่งตัวจัดการไฟล์แล้ว เท่านี้ก็เพียงพอสำหรับงาน terminal ในชีวิตประจำวันส่วนใหญ่ครับ
ตอนต่อไป: terminal 103 — pipes, redirects, and finding things ตรงนั้นคือจุดที่คำสั่งเดี่ยวๆ จะไม่ต้องทำงานลำพังอีกต่อไปครับ เราจะส่ง output ของคำสั่งหนึ่งไปให้อีกคำสั่ง บันทึกผลลัพธ์ลงไฟล์ และค้นทั้งโปรเจ็กต์โดยไม่ต้องไล่คลิกทีละโฟลเดอร์