Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

03

View & Inspect Files

  • Read logs without opening a full editor
  • Confirm file type before editing
  • Watch a log grow (tail -f)
catlessheadtailwcfilestat

Must-know cold

  • less FILE · head -n 20 · tail -n 50 · tail -f LOG
  • cat FILE (small files only) · wc -l FILE
  • file PATH · stat PATH · ls -l PATH

Commands

cat

Definition: Concatenate and print file contents to stdout (best for small files).

OptionArgumentMeaningExample
(none)FILE...Concatenate/print file(s) to stdoutcat f.txt
-nFILENumber all linescat -n f.txt
-bFILENumber non-blank linescat -b f.txt
-AFILEShow non-printing (^I tab, $ end)cat -A f.txt
-sFILESqueeze repeated blank linescat -s f.txt
-vFILEShow non-printing (except tab/NL)cat -v f.txt

Flag combos

ComboMeaningExample
cat -n file | headNumbered previewcat -n app.log | head
cat a b > cConcatenate into new filecat part1 part2 > all

less

Definition: Page through a file interactively (scroll, search, follow).

OptionArgumentMeaningExample
(none)FILEInteractive pager (scroll/search)less /var/log/syslog
-NFILEShow line numbersless -N file
-SFILEChop long lines (no wrap)less -S wide.log
-iFILESearch ignore caseless -i file then /error
-FFILEQuit if one screenless -F short
-RFILERaw ANSI colorsless -R colored.log
+FFILEStart in follow mode (like tail -f)less +F app.log
+/PATFILEOpen at first matchless +/ERROR app.log

Inside less (keys):

  • q quit · /pat search · n next · g top · G end · F follow · Ctrl-C stop follow

Flag combos

ComboMeaningExample
less -NS fileNumbers + chopless -NS access.log

head

Definition: Show the first lines (or bytes) of a file.

OptionArgumentMeaningExample
(none)FILEFirst 10 lines (default)head file
-nN FILEFirst N lineshead -n 20 file
-n-N FILEAll but last N lines (GNU)head -n -5 file
-cN FILEFirst N byteshead -c 100 file
-qFILE...Never print filename headershead -q *.log
-vFILEAlways print headershead -v a b

Flag combos

ComboMeaningExample
head -n 5Quick samplehead -n 5 /etc/passwd

tail

Definition: Show the last lines of a file; with -f, follow live appends.

OptionArgumentMeaningExample
(none)FILELast 10 linestail file
-nN FILELast N linestail -n 50 app.log
-n+N FILEFrom line N to endtail -n +100 file
-cN FILELast N bytestail -c 200 file
-fFILEFollow append (live log)tail -f app.log
-FFILEFollow by name; retry if rotatedtail -F /var/log/app.log
--pidPIDWith -f: stop when PID diestail -f log --pid=1234
-qFILE...No headerstail -q *.log

Flag combos

ComboMeaningExample
tail -n 100 -f logLast 100 then followtail -n100 -f app.log
tail -F logSurvive logrotateProduction agents

wc

Definition: Count lines, words, and/or bytes in input.

OptionArgumentMeaningExample
(none)FILELines, words, byteswc file
-lFILELine count onlywc -l app.log
-wFILEWord countwc -w file
-cFILEByte countwc -c file
-mFILECharacter countwc -m file
-LFILELength of longest line (GNU)wc -L file

Flag combos

ComboMeaningExample
grep ERR log | wc -lCount matchesgrep -c also works

file

Definition: Guess a file’s type from content (magic), not just the name.

OptionArgumentMeaningExample
(none)PATHGuess type from content/magicfile /bin/ls
-bPATHBrief; no filename prefixfile -b x
-iPATHMIME typefile -i doc
-LPATHFollow symlinksfile -L link
-sPATHRead special files (block/char)file -s /dev/sda
-zPATHLook inside compressedfile -z a.gz

Flag combos

ComboMeaningExample
file *Type all in dirQuick triage

stat

Definition: Show detailed inode metadata: size, mode, owner, timestamps.

OptionArgumentMeaningExample
(none)PATHSize, mode, uid/gid, timestamps, inodestat file
-cFORMAT PATHCustom format (GNU)stat -c '%n %s %a' f
-fPATHFilesystem status (like df)stat -f /
-LPATHFollow symlinkstat -L link
-tPATHTerse one linestat -t file

Useful -c formats:

  • %n name · %s size · %a octal mode · %A human mode · %U/%G owner · %y mtime

Flag combos

ComboMeaningExample
stat -c '%a %n' fOctal mode + namePermission checks
stat -c '%y %n' fMtime

Common recipes

GoalCommand
Sample start of loghead -n 20 app.log
Sample end of logtail -n 50 app.log
Live followtail -f app.log
Page a large fileless app.log
Line countwc -l app.log
Is it binary?file ./artifact
Mode + owner + sizestat file or ls -l file

Pitfalls

  • cat huge logs floods the terminal — prefer less / tail.
  • tail -f follows the inode; after rotate use tail -F.
  • file is a guess — not security proof.
  • Binary files: less may warn; don’t cat binaries into an SSH session.

For more details, try man <command> in your terminal.

Previous02 File & Directory OperationsNext04 Permissions & Ownership