terminal 103: pipes, redirects, and finding things terminal 103: pipes, redirects และการค้นหาให้เจอ
combine commands with `|`, save output with `>` and `>>`, and search text without reading every line by hand ต่อคำสั่งเข้าด้วยกันด้วย `|`, บันทึก output ด้วย `>` และ `>>`, และค้นหาข้อความให้เจอโดยไม่ต้องไล่อ่านทีละบรรทัด
in terminal 102 you changed files one command at a time. useful, but still a bit manual.
this lesson is where the terminal starts to feel like a system instead of a list of separate commands.
you’ll do three things:
- send one command’s output into another with a pipe (
|) - save output to a file with redirects (
>and>>) - search and skim text with
grep,head,tail, andhistory
once this clicks, you stop reading everything line by line. you ask the terminal for the part you actually need.
what you’ll know by the end
- how to connect commands with
| - how to save output to a file with
> - how to append new output to the end of a file with
>> - how to search for matching lines with
grep - how to preview just the first or last few lines with
headandtail - how to search your own command history instead of retyping from memory
- the safety rule for this lesson:
>overwrites silently
what this assumes
just terminal 101 and terminal 102.
if pwd, ls, cd, cat, and rm make sense now, you’re ready.
6 ideas that make the terminal much more useful
the pattern is the same on Mac and Windows, but the simulator still keeps a few platform-specific command names:
- Mac:
ls,cat,clear - Windows in this lesson:
dir,type,cls
| what you want to do | Mac | Windows in this lesson | what it means |
|---|---|---|---|
| send output into another command | `ls | grep Desktop` | `dir |
| save output to a new file | ls > files.txt | dir > files.txt | write the output into files.txt |
| add more output to the same file | echo done >> files.txt | echo done >> files.txt | append to the end instead of replacing |
| find matching text | grep invoice notes.txt | grep invoice notes.txt | print lines that contain invoice |
| preview the first lines | head -n 2 notes.txt | head -n 2 notes.txt | show only the top 2 lines |
| preview the last lines | tail -n 1 notes.txt | tail -n 1 notes.txt | show only the bottom line |
two things matter here:
|keeps the output on its way to another command.>and>>send the output into a file instead of showing it on screen.
Output can keep flowing, or it can be saved
try it yourself
walk through this in the terminal below:
ls | grep Desktop— find just the Desktop entry from your home folder Windows:dir | grep Desktopcd Desktop— move into Desktopgrep invoice notes.txt— search the text file for one wordcat notes.txt | head -n 2— show only the first 2 lines Windows:type notes.txt | head -n 2cat notes.txt | tail -n 1— show only the last line Windows:type notes.txt | tail -n 1ls > files.txt— save the current folder listing into a file Windows:dir > files.txtcat files.txt— read the file you just created Windows:type files.txtecho checked >> files.txt— append one more linecat files.txt— confirm the new line was added at the end Windows:type files.txthistory | grep ls— find the earlierlscommands in your own history Windows:history | grep dir
the important moment is step 6:
>does not ask before replacing a file- if
files.txtalready existed, its old contents would be gone
ใน terminal 102 เราจัดการไฟล์ทีละคำสั่งครับ มีประโยชน์แน่นอน แต่ก็ยังเป็นการทำงานแบบทีละชิ้นอยู่
บทนี้คือจุดที่ terminal เริ่มให้ความรู้สึกว่าเป็น “ระบบที่ต่อกันได้” ไม่ใช่แค่รายการคำสั่งแยกกันหลายตัว
เราจะทำ 3 อย่าง:
- ส่ง output ของคำสั่งหนึ่งไปให้อีกคำสั่งด้วย pipe (
|) - บันทึก output ลงไฟล์ด้วย redirect (
>และ>>) - ค้นหาและดูข้อความเฉพาะส่วนที่ต้องการด้วย
grep,head,tail, และhistory
พอเข้าใจตรงนี้ คุณจะเลิกไล่อ่านทุกอย่างเองทีละบรรทัด แล้วเริ่มสั่งให้ terminal คัดสิ่งที่ต้องการออกมาให้แทน
จบโพสต์นี้แล้วจะทำอะไรได้บ้าง
- ต่อคำสั่งเข้าด้วยกันด้วย
| - บันทึก output ลงไฟล์ด้วย
> - เติม output ต่อท้ายไฟล์เดิมด้วย
>> - ค้นหาบรรทัดที่มีคำที่ต้องการด้วย
grep - ดูแค่ไม่กี่บรรทัดแรกหรือท้ายด้วย
headและtail - ค้นประวัติคำสั่งของตัวเองแทนการนั่งพิมพ์ใหม่จากความจำ
- จำกฎสำคัญของบทนี้ได้:
>เขียนทับแบบไม่เตือน
โพสต์นี้เริ่มจากไหน
แค่ผ่าน terminal 101 และ terminal 102 มาก็พอครับ
ถ้า pwd, ls, cd, cat, rm เริ่มคุ้นมือแล้ว ก็ต่อบทนี้ได้เลย
6 แนวคิดที่ทำให้ terminal ใช้งานจริงขึ้นมาก
รูปแบบการคิดเหมือนกันทั้ง Mac และ Windows ครับ แต่ในตัวจำลองนี้ยังมีชื่อคำสั่งบางตัวที่แยกตามแพลตฟอร์มอยู่:
- Mac:
ls,cat,clear - Windows ในบทนี้:
dir,type,cls
| สิ่งที่อยากทำ | Mac | Windows ในบทนี้ | ความหมาย |
|---|---|---|---|
| ส่ง output ไปให้อีกคำสั่ง | `ls | grep Desktop` | `dir |
| บันทึก output ลงไฟล์ใหม่ | ls > files.txt | dir > files.txt | เขียนผลลัพธ์ลง files.txt |
| เติม output ต่อท้ายไฟล์เดิม | echo done >> files.txt | echo done >> files.txt | เติมท้ายไฟล์แทนการเขียนทับ |
| ค้นหาข้อความที่ตรงกัน | grep invoice notes.txt | grep invoice notes.txt | แสดงบรรทัดที่มีคำว่า invoice |
| ดูบรรทัดแรกๆ | head -n 2 notes.txt | head -n 2 notes.txt | ดูแค่ 2 บรรทัดแรก |
| ดูบรรทัดท้ายๆ | tail -n 1 notes.txt | tail -n 1 notes.txt | ดูแค่บรรทัดสุดท้าย |
หัวใจของบทนี้มี 2 อย่าง:
|คือการส่ง output ต่อไปยังอีกคำสั่ง>และ>>คือการส่ง output ลงไฟล์ แทนที่จะให้ขึ้นบนหน้าจอ
Output จะไหลต่อ หรือถูกบันทึกลงไฟล์ก็ได้
ลองเล่นดู
ลองทำตามนี้ใน terminal ด้านล่าง:
ls | grep Desktop— กรองให้เหลือเฉพาะรายการ Desktop จากโฟลเดอร์ home Windows:dir | grep Desktopcd Desktop— เข้าไปใน Desktopgrep invoice notes.txt— ค้นหาคำหนึ่งคำในไฟล์ข้อความcat notes.txt | head -n 2— ดูแค่ 2 บรรทัดแรก Windows:type notes.txt | head -n 2cat notes.txt | tail -n 1— ดูเฉพาะบรรทัดสุดท้าย Windows:type notes.txt | tail -n 1ls > files.txt— บันทึกรายการไฟล์ในโฟลเดอร์นี้ลงไฟล์ Windows:dir > files.txtcat files.txt— อ่านไฟล์ที่เพิ่งสร้าง Windows:type files.txtecho checked >> files.txt— เติมอีกหนึ่งบรรทัดต่อท้ายcat files.txt— ยืนยันว่าบรรทัดใหม่ถูกเติมจริง Windows:type files.txthistory | grep ls— ค้นคำสั่งlsที่คุณเพิ่งพิมพ์ไปจากประวัติคำสั่ง Windows:history | grep dir
จุดที่ต้องจำจริงๆ คือข้อ 6:
>ไม่ถามก่อนเขียนทับ- ถ้ามี
files.txtอยู่แล้ว เนื้อหาเดิมจะหายทันที
how a pipe actually works
read this from left to right:
cat notes.txt | grep invoicecat notes.txtprints the file|grep invoicereceives that output and keeps only the matching line
so the pipe means:
“take what came out of the command on the left, and feed it into the command on the right”
that’s the whole idea.
you can chain more than two commands later, but for beginners, two is enough to build the mental model.
> vs >>
these two look similar, but the behavior is different:
ls > files.txt
echo checked >> files.txt>creates the file if needed, or replaces it if it already exists>>creates the file if needed, or adds to the end if it already exists
if you only remember one safety habit from this lesson, remember this:
pause before pressing Enter on any command with >
it is the same kind of habit as pausing before rm, just for a different reason. rm deletes the file. > deletes the old contents by overwriting them.
why grep, head, tail, and history matter
these are not “advanced hacker commands.” they’re the quickest way to avoid wasting attention.
grepanswers: “which lines mention this word?”headanswers: “show me the beginning, not all of it”tailanswers: “show me the end, not all of it”historyanswers: “what did I run earlier?”
that combination is already enough for a lot of day-to-day terminal work.
one real-world task
imagine you just want a tiny proof file showing what was in your Desktop folder and one note that you checked it.
do this:
cd Desktopls > desktop-list.txtWindows:dir > desktop-list.txtecho reviewed >> desktop-list.txtcat desktop-list.txtWindows:type desktop-list.txtgrep report desktop-list.txt
that’s a very small workflow, but it shows the bigger pattern:
- generate output
- save it
- add one more line
- search the result later
common mistakes
you use > when you meant >>. this is the big one. > replaces. >> appends.
you expect grep here to use full regex power. in this simulator, grep is intentionally plain-text substring matching so the lesson stays simple.
you try to write into a folder. ls > Desktop (or dir > Desktop on Windows) is not “put this in the Desktop folder.” it’s trying to write to a file called Desktop, and the shell should complain because Desktop is a directory.
you forget that head and tail can read from either a file or a pipe. both of these are valid:
head -n 2 notes.txt
cat notes.txt | head -n 2you search everything manually instead of searching your history. if you already typed the command once, try history | grep word before retyping from scratch.
one note about rg
on a real machine, many developers use rg (ripgrep) for fast project-wide searching. this lesson keeps the interactive simulator focused on grep, because the important beginner idea is the pattern:
- search for matching text
- pipe the results
- trim the output down with
headortail
in the next setup-focused lesson, you’ll install tools like rg on a real Mac and start using them outside the simulator.
what this unlocks
now the terminal stops being one-command-at-a-time.
you can connect commands, capture output, search inside it, and keep only the part you need. that’s the workflow layer.
next up: terminal 104 — installing tools on macOS with Homebrew. that’s where commands like rg start showing up for real, and where “command not found” starts making sense instead of feeling random.
pipe ทำงานยังไงจริงๆ
ให้อ่านจากซ้ายไปขวา:
cat notes.txt | grep invoicecat notes.txtแสดงเนื้อหาไฟล์ออกมา|grep invoiceรับ output นั้นไป แล้วเก็บไว้เฉพาะบรรทัดที่มีคำว่าinvoice
ดังนั้น pipe แปลแบบตรงที่สุดได้ว่า:
“เอาผลลัพธ์จากคำสั่งฝั่งซ้าย ส่งต่อให้คำสั่งฝั่งขวา”
แกนของมันมีแค่นี้เลยครับ
ต่อไปคุณจะเห็นคนต่อกันเกิน 2 คำสั่งบ่อยขึ้น แต่สำหรับมือใหม่ แค่ 2 ตัวก็พอให้เห็นภาพแล้ว
> กับ >> ต่างกันยังไง
หน้าตาใกล้กัน แต่ความหมายต่างกัน:
ls > files.txt
echo checked >> files.txt>สร้างไฟล์ถ้ายังไม่มี หรือ เขียนทับ ถ้ามีอยู่แล้ว>>สร้างไฟล์ถ้ายังไม่มี หรือ เติมต่อท้าย ถ้ามีอยู่แล้ว
ถ้าจะจำกฎความปลอดภัยจากบทนี้แค่ข้อเดียว ให้จำข้อนี้ครับ:
หยุดดูคำสั่งก่อนกด Enter ทุกครั้งที่มี >
มันเป็นนิสัยแบบเดียวกับการหยุดดูก่อนใช้ rm แต่คนละเหตุผล
rm ลบไฟล์ ส่วน > ลบเนื้อหาเดิมด้วยการเขียนทับ
ทำไม grep, head, tail, และ history ถึงสำคัญ
คำสั่งพวกนี้ไม่ใช่ “ของสายเทพ” ครับ มันคือวิธีที่เร็วที่สุดในการไม่เสียสมาธิกับข้อมูลที่ไม่จำเป็น
grepตอบคำถามว่า “มีบรรทัดไหนพูดถึงคำนี้บ้าง?”headตอบว่า “ขอดูแค่ต้นๆ ไม่ต้องเอาทั้งหมด”tailตอบว่า “ขอดูท้ายๆ ไม่ต้องเอาทั้งหมด”historyตอบว่า “เมื่อกี้ฉันพิมพ์อะไรไปนะ?”
แค่นี้ก็ครอบคลุมการใช้งาน terminal ในชีวิตประจำวันได้เยอะแล้วครับ
โจทย์จากโลกจริง
สมมติว่าคุณอยากเก็บหลักฐานสั้นๆ ว่าใน Desktop มีอะไรอยู่ แล้วเติมโน้ตไว้หนึ่งบรรทัดว่าเช็กแล้ว
ลองทำแบบนี้:
cd Desktopls > desktop-list.txtWindows:dir > desktop-list.txtecho reviewed >> desktop-list.txtcat desktop-list.txtWindows:type desktop-list.txtgrep report desktop-list.txt
มันเป็น workflow เล็กๆ แต่สะท้อนรูปแบบใหญ่ของการใช้ terminal ได้ชัด:
- สร้าง output
- บันทึกไว้
- เติมข้อมูลอีกนิด
- กลับมาค้นทีหลัง
ข้อผิดพลาดที่พบบ่อย
ใช้ > ทั้งที่จริงควรใช้ >> อันนี้เจอบ่อยที่สุดครับ > เขียนทับ ส่วน >> เติมต่อท้าย
คาดหวังว่า grep ใน simulator นี้จะรองรับ regex เต็มรูปแบบ ในบทนี้ grep ถูกตั้งใจให้เป็นการหาแบบ plain text ธรรมดา เพื่อให้โฟกัสอยู่ที่แนวคิดหลักก่อน
พยายามเขียนลงโฟลเดอร์ ls > Desktop (หรือ dir > Desktop บน Windows) ไม่ได้แปลว่า “เอาไปไว้ในโฟลเดอร์ Desktop”
มันแปลว่า “พยายามเขียนลงไฟล์ชื่อ Desktop” ซึ่ง shell ควรจะฟ้อง เพราะ Desktop เป็นโฟลเดอร์
ลืมว่า head กับ tail รับ input ได้สองทาง ทั้งสองแบบนี้ใช้ได้:
head -n 2 notes.txt
cat notes.txt | head -n 2นั่งพิมพ์คำสั่งซ้ำจากความจำทั้งที่ค้นจาก history ได้ ถ้าคุณเคยพิมพ์ไปแล้ว ลอง history | grep คำที่จำได้ ก่อนพิมพ์ใหม่ตั้งแต่ต้น
ขอพูดถึง rg สักนิด
บนเครื่องจริง นักพัฒนาหลายคนใช้ rg (ripgrep) เพื่อค้นทั้งโปรเจ็กต์ได้เร็วมาก
แต่ในบทนี้ ตัว simulator จะโฟกัสที่ grep ก่อน เพราะสิ่งสำคัญสำหรับมือใหม่คือ “รูปแบบการคิด”
- ค้นหาข้อความที่ตรงกัน
- ส่งผลลัพธ์ต่อด้วย pipe
- ตัดให้เหลือแค่ส่วนที่ต้องการด้วย
headหรือtail
ในบทถัดไปที่พูดเรื่องการติดตั้งเครื่องมือบน Mac เราจะค่อยไปติดตั้งของจริงอย่าง rg แล้วใช้มันนอก simulator กันครับ
เปิดประตูไปต่อที่ไหน
จากนี้ terminal จะไม่ใช่ที่ที่คุณพิมพ์คำสั่งเดี่ยวๆ ทีละอันแล้วจบอีกต่อไป
คุณเริ่มต่อคำสั่งเข้าด้วยกัน เก็บ output ค้นใน output และเก็บไว้เฉพาะส่วนที่ต้องการได้แล้ว นี่คือชั้นของ workflow ครับ
ตอนต่อไปคือ terminal 104 — installing tools on macOS with Homebrew
ตรงนั้นคำสั่งอย่าง rg จะเริ่มมีตัวตนบนเครื่องจริง และคำว่า “command not found” จะเริ่มอธิบายได้อย่างเป็นเหตุเป็นผลมากขึ้น