Viewing Files: cat, less, head, tail

beginner
10 min

Viewing Files: cat, less, head, tail

A huge part of working on Linux is simply reading files: configuration files, logs, source code, CSV exports. You rarely need an editor just to look. This lesson covers the four classic viewers—cat, less, head and tail—plus wc for quick statistics, and shows when each tool is the right choice.

cat: Print the Whole File

cat (concatenate) dumps a file straight to the screen:

bash
cat /etc/hostname cat notes.txt cat -n script.sh # -n adds line numbers cat part1.txt part2.txt # print several files back to back

cat is perfect for short files. For a 10,000-line log it floods your terminal—that is what less is for. True to its name, cat can also concatenate files together, which becomes powerful once you learn redirection: cat part1.txt part2.txt > whole.txt.

less: Scroll Through Anything

less opens a file in a scrollable, searchable viewer without loading the whole file into memory, so it handles gigabyte logs effortlessly:

bash
less /var/log/syslog

Essential keys inside less:

  • Space / b – page down / page up
  • Arrow keys – scroll line by line
  • g / G – jump to the start / end of the file
  • /error – search forward for "error"; n for next match, N for previous
  • q – quit

If a command like git log or man ls drops you into a scrolling view, that is less—the same keys apply everywhere.

head: The First Lines

head shows the beginning of a file, ten lines by default:

bash
head /etc/passwd head -n 3 data.csv # just the first 3 lines head -c 100 file.bin # first 100 bytes

head -n 1 data.csv is the classic way to peek at a CSV header row before processing the file.

tail: The Last Lines

tail is the mirror image, showing the end of a file—which is where the newest entries of any log live:

bash
tail /var/log/syslog tail -n 50 app.log # last 50 lines tail -f app.log # follow: stream new lines as they arrive tail -f app.log | grep -i error # follow, but only show errors

tail -f (follow) is one of the most used commands in real operations work: start it, trigger your application, and watch log lines appear live. Press Ctrl+C to stop following.

wc: Count Lines, Words, Bytes

bash
wc -l access.log # number of lines wc -w essay.txt # number of words wc -c photo.jpg # number of bytes

wc -l answers questions like "how many entries are in this log?" instantly.

Choosing the Right Tool

| Situation | Tool | |---|---| | Short config file | cat | | Long file, need to search or scroll | less | | Check a CSV header or file format | head | | Latest log entries | tail -n 50 | | Watch a log live | tail -f | | How big is this file? | wc -l or ls -lh |

Practice Session

bash
cat /etc/os-release head -n 5 /etc/passwd tail -n 5 /etc/passwd wc -l /etc/passwd less /etc/services # practice /search, g, G, then q to quit

All of these files are safe, world-readable system files—perfect practice material on any Linux machine.

Key Takeaways

  • cat prints whole files; ideal for short ones and for concatenating.
  • less is a fast, searchable pager: / to search, q to quit.
  • head and tail show the start and end; -n controls how many lines.
  • tail -f streams a growing log in real time—indispensable for debugging.
  • wc -l counts lines for instant file statistics.

Next lesson: File Permissions and chmod — understand who can read, write and execute your files.

Viewing Files: cat, less, head, tail - Linux & Command Line | CodeYourCraft | CodeYourCraft