C.W.K.
Stream
Lesson 01 of 10 · published

cat, less, more

~8 min · cat, less, more, pager

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Three ways to read a file

cat dumps the file straight to stdout. less is a pager — interactive, scrollable, searchable. more is the older simpler pager that less replaced (yes, the joke less is more is real).

cat — concatenate

  • cat file — print contents.
  • cat a b c — concatenate three files in order.
  • cat -n file — number every line.
  • cat -A file — show non-printing characters (tabs as ^I, line endings as $). Useful when a file looks fine but a tool says "unexpected token."

Don't pipe cat when you don't have to — grep foo file is cleaner than cat file | grep foo. The famous useless use of cat award.

less — the right pager

Inside less you can navigate without loading the whole file:

  • Space / b — page down / up
  • j / k — line down / up (vim-style)
  • g / G — top / bottom
  • /pattern — search forward, ?pattern backward
  • n / N — next / previous match
  • q — quit
  • F — follow mode (like tail -f)

Less is the default pager

When man, git log, or git diff open, they're piping into less. Set $PAGER to less -R (raw colors) so colored output stays colored. $LESS="-RFX" is a common dotfile setting.

Code

When cat is fine vs UUOC·bash
# Useless use of cat
cat file | grep foo
# Direct
grep foo file
# But cat is fine for inspecting tabs/newlines
cat -A weird.txt

External links

Exercise

Open ~/.zshrc with less ~/.zshrc. Use /export to search, n for next match, q to quit. Then cat -A ~/.zshrc | head and notice tab/EOL markers. Add LESS='-RFX' to your shell config and re-test.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.