All Modules / Module 3 / Lesson 2 of 4

Reading file contents without opening them

4 minute read

Look Inside Files Without Opening Them

Sometimes you just want to see what's in a file — without opening an app, without leaving Terminal.

There are several commands for this, depending on what you need.

cat: Show Everything

cat dumps the entire contents of a file to your screen:

1

View a text file:

cat ~/.zshrc

The entire file scrolls past. Good for short files. For long files, it's overwhelming.

What does "cat" stand for? "Concatenate." Originally meant for joining files together, but mostly used to display file contents.

head: Just the Beginning

head shows just the first 10 lines:

1

View the first 10 lines:

head ~/.zshrc
2

Or specify how many lines:

head -20 ~/.zshrc

tail: Just the End

tail shows the last 10 lines. Useful for log files where the newest entries are at the bottom:

1

View the last 10 lines:

tail ~/.zshrc

less: Browse a Long File

less opens a file in a scrollable viewer. You can move up and down:

1

Open a file in the viewer:

less ~/.zshrc
2

Use / or Space to scroll

3

Press q to quit

Key Takeaway

cat for short files, head/tail for beginnings/endings, less for browsing long files. Press q to exit less.