Linux Command Zero to Hero - Episode 4: Melihat & Edit File
Linux Command Zero to Hero - Episode 4: Melihat & Edit File
APA YANG AKAN KITA PELAJARI?
Di episode 1-3, kita sudah belajar:
- Navigasi file system (ls, cd, tree)
- Manipulasi file & directory (mkdir, cp, mv, rm)
Sekarang saatnya belajar cara MELIHAT ISI file dan MENGEDITnya!
Di episode keempat ini, kita akan mempelajari:
1. cat - Tampilkan seluruh isi file
2. less - Viewer file interaktif (scroll, search)
3. head - Tampilkan baris-baris pertama
4. tail - Tampilkan baris-baris terakhir (+ live monitoring!)
5. tac - Tampilkan file terbalik (dari bawah ke atas)
6. nano - Text editor sederhana untuk pemula
7. file - Deteksi tipe file
8. wc - Hitung baris, kata, dan karakter
9. more - Alternatif less (basic viewer)
episode ini, kamu bisa membaca, melihat, dan mengedit file apapun di Linux!
FILE YANG AKAN KITA GUNAKAN
Supaya tidak bingung nantinya kita akan gunakan 3 file ini ya:
- notes.txt (file teks biasa)
- report.csv (file data)
- script.sh (file script)
COMMAND 1: cat - CONCATENATE & DISPLAY
cat adalah command paling basic untuk MELIHAT ISI FILE.
1.1 cat - Tampilkan Isi File
Command sederhana:
[code]cat namafilenya.txt[/code]
Contoh:
[code]cat notes.txt[/code]
Output:
Hello World
This is a test file
Linux is awesome!
Yang terjadi:
- Seluruh isi file ditampilkan di terminal
- Langsung keluar setelah selesai
- Nggak bisa scroll ke atas (kecuali terminal support scroll)
Kapan dipakai?
- File KECIL (di bawah 50 baris)
- Quick check isi file
- Lihat konfigurasi singkat
JANGAN dipakai untuk:
- File BESAR (ratusan/ribuan baris) → terminal bakal penuh!
- File log yang panjang → gunakan less atau tail
1.2 cat -n - Dengan Nomor Baris
Tampilkan isi file dengan nomor baris:
[code]cat -n namafilenya.txt[/code]
Contoh:
[code]cat -n notes.txt[/code]
Output:
1 Hello World
2 This is a test file
3 Linux is awesome!
4 Fourth line
5 Fifth line
Kapan dipakai?
- Referensi baris spesifik
- Debugging script (lihat baris error)
- Dokumentasi (tunjuk baris tertentu)
- Code review
1.3 cat -b - Nomor Baris Hanya untuk Baris yang Ada Isi
Sama seperti -n, tapi baris kosong nggak dinomori:
[code]cat -b namafilenya.txt[/code]
Contoh:
[code]cat -b notes.txt[/code]
Output:
1 Hello World
2 This is a test file
3 Linux is awesome!
4 Fourth line
Perhatikan: baris kosong (antara line 2 dan 3) nggak punya nomor.
Kapan dipakai?
- File dengan banyak baris kosong
- Lebih clean dari cat -n
- Dokumentasi code
1.4 cat -E - Tampilkan Akhir Baris
Tampilkan karakter $ di setiap akhir baris:
[code]cat -E namafilenya.txt[/code]
Output:
Hello World$
This is a test file$
Linux is awesome!$
Kapan dipakai?
- Debugging whitespace
- Cek ada trailing spaces atau nggak
- Troubleshooting format file
1.5 cat - Gabungkan Banyak File
cat bisa gabungkan beberapa file sekaligus:
[code]cat file1.txt file2.txt[/code]
Output: Isi file1 dulu, lalu isi file2.
Contoh:
[code]cat header.txt content.txt footer.txt > full_document.txt[/code]
Ini akan:
- Gabungkan header.txt + content.txt + footer.txt
- Simpan hasilnya ke full_document.txt
Kapan dipakai?
- Gabungkan file log
- Build document dari bagian-bagian
- Merge data files
1.6 cat > - Buat File Baru (Quick Edit)
Buat file baru dan langsung isi:
[code]cat > namafilenya.txt[/code]
Setelah Enter:
- Terminal masuk mode input
- Ketik isi file baris per baris
- Tekan Ctrl + D untuk selesai dan simpan
Contoh:
[code]cat > notes.txt[/code]
Hello World
This is my note
Ctrl + D (untuk simpan)
Cek hasilnya:
cat notes.txt
→ Hello World
→ This is my note
Kapan dipakai?
- Buat file kecil dengan cepat
- Nggak mau buka text editor
- Quick note
Catatan:
- Kalau file sudah ada, isinya akan DI-TIMPA (overwrite)!
- Pakai cat >> untuk append (tambah di akhir)
1.7 cat >> - Append ke File (Tambah di Akhir)
Tambahkan isi ke file yang sudah ada:
[code]cat >> namafilenya.txt[/code]
Contoh:
[code]cat >> notes.txt[/code]
New line added
Another line
Ctrl + D
Cek hasilnya:
[code]cat notes.txt[/code]
→ Hello World (isi lama)
→ This is my note (isi lama)
→ New line added (baru!)
→ Another line (baru!)
Perbedaan:
- cat > file.txt = overwrite (timpa semua)
- cat >> file.txt = append (tambah di akhir)
Kapan dipakai?
- Tambah catatan ke file yang ada
- Append log entries
- Update file tanpa hapus isi lama
1.8 cat dengan Multiple Files dan Pattern
Gabungkan semua file dengan pattern:
[code]cat *.txt[/code]
→ Gabungkan semua file .txt
[code]cat /var/log/*.log[/code]
→ Tampilkan semua log files
Kapan dipakai?
- Gabungkan banyak file sekaligus
- Merge log files
- Combine data files
COMMAND 2: less - INTERACTIVE FILE VIEWER
less adalah viewer file yang INTERAKTIF. Bisa scroll, search, dan navigate.
2.1 less - Buka File
Command sederhana:
[code]less namafilenya.txt[/code]
Contoh:
[code]less report.csv[/code]
Yang terjadi:
- File dibuka di terminal
- Tampilan satu layar penuh
- Bisa scroll naik-turun
- Bisa search keyword
- Tekan q untuk keluar
Kapan dipakai?
- File BESAR (ratusan/ribuan baris)
- Baca log files
- Baca dokumentasi panjang
- PALING SERING dipakai untuk baca file!
Perbedaan cat vs less:
- cat = tampilkan semua langsung (file kecil)
- less = interactive viewer, bisa scroll (file besar)
2.2 less - Navigasi
Setelah buka file dengan less, gunakan tombol ini:
Navigasi Baris:
- ↓ atau j = turun 1 baris
- ↑ atau k = naik 1 baris
- Enter = turun 1 baris
Navigasi Halaman:
- Space = turun 1 halaman
- b = naik 1 halaman (back)
- Page Down = turun 1 halaman
- Page Up = naik 1 halaman
Keluar:
- q = quit (keluar)
Kapan dipakai?
- SELALU saat pakai less!
- Navigasi file panjang
2.3 less - Search/ Cari Text
Cari text dalam file:
[code]/keyword[/code]
Contoh:
[code]/error[/code]
→ Cari semua baris yang mengandung "error"
Setelah ketemu:
- n = next (cari yang berikutnya)
- N (Shift + n) = previous (cari yang sebelumnya)
Cari dari bawah ke atas:
[code]?keyword[/code]
Contoh:
[code]?warning[/code]
→ Cari "warning" dari bawah ke atas
Kapan dipakai?
- Cari error di log file
- Cari keyword tertentu
- Debugging (cari pesan error)
- Sangat BERGUNA untuk log files!
2.4 less - Tampilkan Nomor Baris
Buka file dengan nomor baris:
[code]less -N namafilenya.txt[/code]
Sekarang setiap baris ada nomornya.
Kapan dipakai?
- Referensi baris spesifik
- Debugging
- Code review
2.5 less - Multiple Files
Buka banyak file sekaligus:
[code]less file1.txt file2.txt file3.txt[/code]
Navigasi antar file:
- :n = next file (file berikutnya)
- :p = previous file (file sebelumnya)
- :d = delete current file (tutup file ini)
Kapan dipakai?
- Baca banyak log files
- Compare beberapa file
- Review dokumentasi multi-file
2.6 less +F - Follow Mode (Live Monitoring)
Monitor file secara real-time (seperti tail -f):
[code]less +F namafilenya.log[/code]
Yang terjadi:
- less masuk mode "follow"
- Otomatis scroll ke bawah
- Update otomatis kalau ada baris baru
- Seperti tail -f tapi bisa scroll ke atas!
Keluar dari follow mode:
- Ctrl + C
- Lalu bisa scroll normal
- Tekan F lagi untuk masuk follow mode lagi
Kapan dipakai?
- Monitor log file secara real-time
- Debugging aplikasi yang sedang jalan
- PALING BERGUNA untuk monitoring!
Pro Tip:
less +F lebih powerful dari tail -f karena bisa scroll ke atas untuk lihat history!
2.7 less dengan Pipe
less bisa menerima input dari command lain:
[code]cat /var/log/syslog | less[/code]
→ Baca log file dengan less
ps aux | less
→ List proses dengan scroll
[code]cat file.txt | grep "error" | less[/code]
→ Filter dulu, lalu lihat hasilnya
Kapan dipakai?
- Output command yang panjang
- Filter + view
- Any command yang output-nya banyak!
COMMAND 3: head - BARIS PERTAMA
head menampilkan baris-baris PERTAMA dari file.
3.1 head - Basic (10 Baris Pertama)
Command default:
[code]head namafilenya.txt[/code]
Default: menampilkan 10 baris pertama.
Contoh:
[code]head report.csv[/code]
Output (10 baris pertama)
Kapan dipakai?
- Quick preview file
- Cek header CSV/JSON
- Lihat awal log file
- File besar, cuma mau lihat awalnya
3.2 head -n - Jumlah Baris Spesifik
Tampilkan jumlah baris yang kita tentukan:
[code]head -n 20 namafilenya.txt[/code]
Contoh:
[code]head -n 5 report.csv[/code]
→ 5 baris pertama
Kapan dipakai?
- Preview file dengan jumlah custom
- Ambil header CSV (head -n 1)
- Sample data dari file besar
3.3 head - Shorthand (Tanpa -n)
Bisa juga tanpa -n:
[code]head -5 file.txt[/code]
→ Sama dengan head -n 5
Kapan dipakai?
- Lebih cepat diketik
- Personal preference
3.4 head - Multiple Files
Tampilkan awal dari banyak file:
[code]head file1.txt file2.txt[/code]
Output:
==> file1.txt <==
(barisan pertama file1)
==> file2.txt <==
(barisan pertama file2)
Dengan jumlah baris:
[code]head -n 5 file1.txt file2.txt[/code]
Kapan dipakai?
- Compare awal beberapa file
- Preview banyak file sekaligus
3.5 head dengan Pipe
head bisa menerima input dari command lain:
[code]ps aux | head -20[/code]
→ 20 proses pertama
[code]cat /var/log/syslog | head -50[/code]
→ 50 baris pertama log
Kapan dipakai?
- Batasi output command
- Sample data
- Quick preview
COMMAND 4: tail - BARIS TERAKHIR
tail menampilkan baris-baris TERAKHIR dari file.
4.1 tail - Basic (10 Baris Terakhir)
Command default:
[code]tail namafilenya.txt[/code]
Default: menampilkan 10 baris terakhir.
Contoh:
[code]tail report.csv[/code]
Output (10 baris terakhir)
Kapan dipakai?
- Lihat data terbaru di file
- Cek akhir log file
- File yang terus di-append (log, data)
Perbedaan head vs tail:
- head = dari ATAS (awal file)
- tail = dari BAWAH (akhir file)
4.2 tail -n - Jumlah Baris Spesifik
Tampilkan jumlah baris yang kita tentukan:
[code]tail -n 20 namafilenya.txt[/code]
Contoh:
[code]tail -n 5 report.csv[/code]
→ 5 baris terakhir
[code]tail -n 50 /var/log/syslog[/code]
→ 50 baris terakhir log
Kapan dipakai?
- Lihat recent entries di log
- Cek data terbaru
- Monitor file yang terus bertambah
4.3 tail -f - Follow Mode (Live Monitoring) ⭐ SUPER PENTING!
Monitor file secara REAL-TIME:
[code]tail -f namafilenya.log[/code]
Yang terjadi:
- Terminal terus "listening" file
- Setiap ada baris baru → langsung ditampilkan
- Seperti "live feed"
Contoh penggunaan paling umum:
[code]tail -f /var/log/syslog[/code]
→ Monitor log sistem secara live
Keluar:
Ctrl + C
Kapan dipakai?
- Debugging aplikasi yang sedang jalan
- Monitor log secara real-time
- PALING SERING dipakai untuk troubleshooting!
- Developer WAJIB tahu ini!
4.4 tail -F - Follow + Retry (Lebih Power!)
Sama seperti -f, tapi lebih robust:
[code]tail -F namafilenya.log[/code]
Perbedaan dengan -f:
- -f = follow biasa
- -F = follow + retry kalau file di-rename atau di-recreate
Scenario di mana -F berguna:
1. Aplikasi menulis ke app.log
2. Log rotation: app.log di-rename jadi app.log.1
3. Aplikasi buat app.log baru
4. tail -F otomatis follow file BARU
5. tail -f akan kehilangan track!
Kapan dipakai?
- Monitor log files di production
- Log rotation aktif
- Monitoring jangka panjang
Pro Tip: Selalu pakai tail -F untuk production monitoring!
4.5 tail -n + - Dari Baris ke-X Sampai Akhir
Tampilkan dari baris tertentu sampai akhir:
[code]tail -n +50 namafilenya.txt[/code]
→ Dari baris 50 sampai akhir
Contoh:
[code]tail -n +100 large_file.txt[/code]
→ Skip 99 baris pertama, tampilkan sisanya
Kapan dipakai?
- Skip header file
- Lihat bagian tengah file
- Process file besar sebagian
4.6 tail - Multiple Files
Tampilkan akhir dari banyak file:
[code]tail file1.txt file2.txt[/code]
Output:
==> file1.txt <==
(barisan terakhir file1)
==> file2.txt <==
(barisan terakhir file2)
Dengan follow mode:
tail -f file1.log file2.log
→ Monitor 2 log files sekaligus
Kapan dipakai?
- Monitor multiple log files
- Compare akhir beberapa file
4.7 tail dengan Pipe
tail bisa menerima input dari command lain:
[code]ps aux | tail -10[/code]
→ 10 proses terakhir
[code]ls -lt | tail -5[/code]
→ 5 file paling lama (karena ls -lt sort newest first)
Kapan dipakai?
- Filter + lihat hasil terbaru
- Sample data dari bawah
- Get last N results
COMMAND 5: tac - FILE TERBALIK
tac adalah kebalikan dari cat (tac = cat terbalik!).
5.1 tac - Tampilkan File dari Bawah ke Atas
Command sederhana:
[code]tac namafilenya.txt[/code]
Contoh:
[code]cat notes.txt[/code]
Kapan dipakai?
- Baca log dari yang terbaru (paling bawah = paling baru)
- Reverse data
- Debugging (lihat dari akhir ke awal)
5.2 tac dengan Pipe
tac bisa dipakai di pipe:
[code]cat /var/log/syslog | tac | head -20[/code]
→ 20 baris TERBARU dari log (karena tac membalik, head ambil dari atas)
Alternatif dari tail:
[code]tail -20 /var/log/syslog[/code]
→ 20 baris terakhir
Kapan dipakai?
- Lihat log terbaru tapi urut dari yang paling baru
- Reverse sort data
COMMAND 6: file - DETEKSI TIPE FILE
file mendeteksi tipe/jenis file berdasarkan isinya.
6.1 file - Deteksi Satu File
Command sederhana:
[code]file namafilenya[/code]
Contoh:
[code]file notes.txt[/code]
Output: notes.txt: ASCII text
[code]file photo.jpg[/code]
Output: photo.jpg: JPEG image data
Kapan dipakai?
- Cek tipe file tanpa extension
- Debugging file yang salah format
- Identifikasi file misterius
6.2 file - Deteksi Banyak File
[code]file * [/code]
→ Deteksi semua file di folder saat ini
[code]file /path/to/files/*[/code]
→ Deteksi semua file di folder tertentu
Kapan dipakai?
- Audit folder
- Cek tipe banyak file sekaligus
- Identifikasi file tanpa extension
6.3 file - Output Singkat
[code]file -b namafilenya[/code]
→ Hanya tipe file, tanpa nama file
Contoh:
file -b notes.txt
Output: ASCII text
(dibandingkan)
[code]file notes.txt[/code]
Output: notes.txt: ASCII text
Kapan dipakai?
- Scripting
- Processing file types
- Clean output
COMMAND 7: wc - WORD COUNT
wc menghitung baris, kata, dan karakter di file.
7.1 wc - Basic (Baris, Kata, Karakter)
Command default:
[code]wc namafilenya.txt[/code]
Output: 3 angka + nama file
10 50 300 notes.txt
↑ ↑ ↑
│ │ └── Jumlah karakter (bytes)
│ └────── Jumlah kata
└────────── Jumlah baris
Kapan dipakai?
- Quick stats file
- Cek ukuran file dalam baris
- Count words
7.2 wc -l - Hitung Baris
wc -l namafilenya.txt
Output:
10 notes.txt
Contoh penggunaan paling umum:
[code]cat /var/log/syslog | wc -l[/code]
→ Berapa baris log?
[code]ps aux | wc -l[/code]
→ Berapa proses yang jalan?
Kapan dipakai?
- PALING SERING dipakai!
- Count lines di file besar
- Hitung jumlah data
- Quick statistics
7.3 wc -w - Hitung Kata
[code]wc -w namafilenya.txt[/code]
Output:
50 notes.txt
Kapan dipakai?
- Hitung jumlah kata di dokumen
- Word count untuk artikel
- Content statistics
7.4 wc -c - Hitung Karakter/Bytes
[code]wc -c namafilenya.txt[/code]
Output:
300 notes.txt
Kapan dipakai?
- Cek ukuran file dalam bytes
- File size comparison
- Data size verification
7.5 wc -m - Hitung Karakter
[code]wc -m namafilenya.txt[/code]
Sama seperti -c, tapi untuk multi-byte characters (UTF-8).
Kapan dipakai?
- File dengan karakter unicode
- Count actual characters
7.6 wc - Multiple Files
[code]wc file1.txt file2.txt file3.txt[/code]
Output:
10 50 300 file1.txt
20 100 600 file2.txt
5 25 150 file3.txt
35 175 1050 total
Baris "total" = jumlah semua file.
Kapan dipakai?
- Compare ukuran file
- Total statistics
- Batch file analysis
7.7 wc dengan Pipe
wc bisa menerima input dari command lain:
[code]ls -la | wc -l[/code]
→ Berapa file di folder?
[code]cat /var/log/syslog | grep "error" | wc -l[/code]
→ Berapa error di log?
Kapan dipakai?
- Count hasil filter
- Statistics dari command lain
- Very powerful combination!
COMMAND 8: more - BASIC FILE VIEWER
more adalah viewer file yang lebih simple dari less.
8.1 more - Basic Usage
Command sederhana:
[code]more namafilenya.txt[/code]
Navigasi:
- Space = halaman berikutnya
- Enter = baris berikutnya
- q = quit (keluar)
Kapan dipakai?
- Quick view file
- Alternatif less (lebih simple)
- Older systems (more lebih universal)
Perbedaan more vs less:
- more = cuma bisa forward (maju)
- less = bisa forward DAN backward (maju-mundur)
- less lebih powerful, lebih disarankan!