Piping & Redirection — Terminal

Piping & Redirection Piping dan redirection adalah fitur yang membuat terminal sangat powerful. Mereka memungkinkan kamu menghubungkan perintah-perintah kecil m

Piping & Redirection

Piping dan redirection adalah fitur yang membuat terminal sangat powerful. Mereka memungkinkan kamu menghubungkan perintah-perintah kecil menjadi operasi yang kompleks.

Aliran Data: stdin, stdout, stderr

Setiap proses punya tiga "saluran" data — memahami diagram ini membuat piping dan redirection jadi intuitif:

<style> .box { fill: #f9fafb; stroke: #111827; stroke-width: 1.5; } .cmd { fill: #fef3c7; stroke: #b45309; stroke-width: 1.5; } .file { fill: #dbeafe; stroke: #1e40af; stroke-width: 1.5; } .stream-stdout { stroke: #047857; stroke-width: 2.5; fill: none; } .stream-stderr { stroke: #b91c1c; stroke-width: 2.5; fill: none; stroke-dasharray: 6 3; } .label { font: 600 13px ui-sans-serif, system-ui; fill: #111827; } .small { font: 500 11px ui-sans-serif, system-ui; fill: #374151; } .arrow-stdout { fill: #047857; } .arrow-stderr { fill: #b91c1c; } </style> command A (produces output) 1=stdout 2=stderr command B (via pipe |) stdout.log errors.log combined.log 1 | (pipe) 1> file 2> file 2>&1 Legenda: stdout (1) stderr (2) tee: stdout ke terminal + file bersamaan (T-junction) Contoh: cmd 2>&1 | tee log.txt → gabung stderr ke stdout, pipa ke tee, tee duplikat ke layar + file

Pipe (|)

Pipe mengambil output dari satu perintah dan menjadikannya input untuk perintah berikutnya:

# Tanpa pipe — output ls langsung ke layar
ls -la

# Dengan pipe — output ls menjadi input grep
ls -la | grep ".js"
# Hanya tampilkan file .js dari listing

# Chain beberapa pipe
cat access.log | grep "ERROR" | wc -l
# 1. Baca file log
# 2. Filter hanya baris ERROR
# 3. Hitung jumlah baris

Contoh Pipe yang Berguna

# 10 file terbesar di folder saat ini
du -sh * | sort -rh | head -10

# Proses yang paling banyak makan memory
ps aux | sort -k 4 -rn | head -5

# Hitung berapa kali setiap HTTP method muncul di log
grep -oE "(GET|POST|PUT|DELETE)" access.log | sort | uniq -c | sort -rn

# Cari dependency di package.json
cat package.json | grep -E "\"(react|vue|angular)"

# Daftar semua file .js urutkan berdasarkan ukuran
find . -name "*.js" -not -path "*/node_modules/*" | xargs wc -l | sort -n

Output Redirection

> — Redirect ke File (Overwrite)

# Simpan output ke file (menimpa isi sebelumnya!)
ls -la > daftar-file.txt

# Simpan hasil pencarian
grep -rn "TODO" src/ > todo-list.txt

# Buat file baru dengan isi tertentu
echo "Hello World" > hello.txt

>> — Redirect ke File (Append)

# Tambahkan ke akhir file (tidak menimpa)
echo "Log entry 1" >> app.log
echo "Log entry 2" >> app.log

# Gabungkan beberapa file
cat header.txt >> combined.txt
cat content.txt >> combined.txt

< — Input dari File

# Kirim isi file sebagai input
sort < unsorted.txt

# Sama dengan: cat unsorted.txt | sort
# Tapi < lebih efisien karena tidak perlu proses cat

Error Redirection

Di Unix, ada 3 stream:

# Redirect error ke file
npm install 2> error.log

# Redirect output DAN error ke file yang sama
npm install > output.log 2>&1
# atau syntax modern:
npm install &> output.log

# Buang error (kirim ke /dev/null)
find / -name "*.conf" 2>/dev/null

# Pisahkan output dan error
npm run build > build-output.log 2> build-errors.log

/dev/null — Black Hole

# Buang semua output (tidak ingin lihat)
npm install > /dev/null 2>&1

# Hanya buang error
find / -name "*.log" 2>/dev/null

tee — Output ke Layar DAN File

# Tampilkan di layar sekaligus simpan ke file
npm run build | tee build.log

# Append ke file
npm run build | tee -a build.log

xargs — Ubah Input Menjadi Argumen

# Hapus semua file .tmp yang ditemukan
find . -name "*.tmp" | xargs rm

# Hitung baris di semua file JavaScript
find . -name "*.js" -not -path "*/node_modules/*" | xargs wc -l

# Buka semua file yang mengandung "TODO" di editor
grep -rl "TODO" src/ | xargs code

Contoh Workflow

# Cari 5 file terbesar di project
find . -type f -not -path "*/node_modules/*" | xargs du -sh 2>/dev/null | sort -rh | head -5

# Backup: compress semua file .js ke archive
find . -name "*.js" | xargs tar czf backup.tar.gz

# Monitoring: simpan log sambil menonton
tail -f server.log | tee -a archive.log | grep "ERROR"

Yang akan kamu pelajari