mervia@learn:~/workshops/terminal-10x/103$ ← /learn
· Pichaya Puttekulangkura พิชยะ พุฒิกุลางกูร · #terminal#beginners
reading mode โหมดอ่าน

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, and history

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 head and tail
  • 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 doMacWindows in this lessonwhat it means
send output into another command`lsgrep Desktop``dir
save output to a new filels > files.txtdir > files.txtwrite the output into files.txt
add more output to the same fileecho done >> files.txtecho done >> files.txtappend to the end instead of replacing
find matching textgrep invoice notes.txtgrep invoice notes.txtprint lines that contain invoice
preview the first lineshead -n 2 notes.txthead -n 2 notes.txtshow only the top 2 lines
preview the last linestail -n 1 notes.txttail -n 1 notes.txtshow only the bottom line

two things matter here:

  1. | keeps the output on its way to another command.
  2. > and >> send the output into a file instead of showing it on screen.
flow map

Output can keep flowing, or it can be saved

Pipes keep data moving between commands. Redirects stop the flow and write it into a file.

try it yourself

walk through this in the terminal below:

  1. ls | grep Desktop — find just the Desktop entry from your home folder Windows: dir | grep Desktop
  2. cd Desktop — move into Desktop
  3. grep invoice notes.txt — search the text file for one word
  4. cat notes.txt | head -n 2 — show only the first 2 lines Windows: type notes.txt | head -n 2
  5. cat notes.txt | tail -n 1 — show only the last line Windows: type notes.txt | tail -n 1
  6. ls > files.txt — save the current folder listing into a file Windows: dir > files.txt
  7. cat files.txt — read the file you just created Windows: type files.txt
  8. echo checked >> files.txt — append one more line
  9. cat files.txt — confirm the new line was added at the end Windows: type files.txt
  10. history | grep ls — find the earlier ls commands in your own history Windows: history | grep dir

the important moment is step 6:

  • > does not ask before replacing a file
  • if files.txt already 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
สิ่งที่อยากทำMacWindows ในบทนี้ความหมาย
ส่ง output ไปให้อีกคำสั่ง`lsgrep Desktop``dir
บันทึก output ลงไฟล์ใหม่ls > files.txtdir > files.txtเขียนผลลัพธ์ลง files.txt
เติม output ต่อท้ายไฟล์เดิมecho done >> files.txtecho done >> files.txtเติมท้ายไฟล์แทนการเขียนทับ
ค้นหาข้อความที่ตรงกันgrep invoice notes.txtgrep invoice notes.txtแสดงบรรทัดที่มีคำว่า invoice
ดูบรรทัดแรกๆhead -n 2 notes.txthead -n 2 notes.txtดูแค่ 2 บรรทัดแรก
ดูบรรทัดท้ายๆtail -n 1 notes.txttail -n 1 notes.txtดูแค่บรรทัดสุดท้าย

หัวใจของบทนี้มี 2 อย่าง:

  1. | คือการส่ง output ต่อไปยังอีกคำสั่ง
  2. > และ >> คือการส่ง output ลงไฟล์ แทนที่จะให้ขึ้นบนหน้าจอ
แผนภาพการไหล

Output จะไหลต่อ หรือถูกบันทึกลงไฟล์ก็ได้

pipe ทำให้ข้อมูลไหลจากคำสั่งหนึ่งไปอีกคำสั่ง ส่วน redirect หยุดการไหลแล้วเขียนผลลัพธ์ลงไฟล์

ลองเล่นดู

ลองทำตามนี้ใน terminal ด้านล่าง:

  1. ls | grep Desktop — กรองให้เหลือเฉพาะรายการ Desktop จากโฟลเดอร์ home Windows: dir | grep Desktop
  2. cd Desktop — เข้าไปใน Desktop
  3. grep invoice notes.txt — ค้นหาคำหนึ่งคำในไฟล์ข้อความ
  4. cat notes.txt | head -n 2 — ดูแค่ 2 บรรทัดแรก Windows: type notes.txt | head -n 2
  5. cat notes.txt | tail -n 1 — ดูเฉพาะบรรทัดสุดท้าย Windows: type notes.txt | tail -n 1
  6. ls > files.txt — บันทึกรายการไฟล์ในโฟลเดอร์นี้ลงไฟล์ Windows: dir > files.txt
  7. cat files.txt — อ่านไฟล์ที่เพิ่งสร้าง Windows: type files.txt
  8. echo checked >> files.txt — เติมอีกหนึ่งบรรทัดต่อท้าย
  9. cat files.txt — ยืนยันว่าบรรทัดใหม่ถูกเติมจริง Windows: type files.txt
  10. history | 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 invoice
  • cat notes.txt prints the file
  • |
  • grep invoice receives 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.

  • grep answers: “which lines mention this word?”
  • head answers: “show me the beginning, not all of it”
  • tail answers: “show me the end, not all of it”
  • history answers: “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:

  1. cd Desktop
  2. ls > desktop-list.txt Windows: dir > desktop-list.txt
  3. echo reviewed >> desktop-list.txt
  4. cat desktop-list.txt Windows: type desktop-list.txt
  5. grep 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 2

you 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 head or tail

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 invoice
  • cat 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 มีอะไรอยู่ แล้วเติมโน้ตไว้หนึ่งบรรทัดว่าเช็กแล้ว

ลองทำแบบนี้:

  1. cd Desktop
  2. ls > desktop-list.txt Windows: dir > desktop-list.txt
  3. echo reviewed >> desktop-list.txt
  4. cat desktop-list.txt Windows: type desktop-list.txt
  5. grep 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” จะเริ่มอธิบายได้อย่างเป็นเหตุเป็นผลมากขึ้น

$ share

if this lesson helped, share the page with someone learning this topic. ถ้าโพสต์นี้มีประโยชน์ ส่งต่อหน้านี้ให้คนที่กำลังเรียนเรื่องนี้อยู่ได้เลย

share on facebook แชร์ไป Facebook

← back to all lessons ← กลับหน้าหลัก