Looking Inside Files: cat, less, head, and tail

I said "look around the root directory", but I left out some important tools for doing that. You already know about listing the contents of directories with ls, but what if you want to look at the contents of an individual file?

In a graphical file manager, you might double-click an icon and let your desktop environment decide the appropriate application for opening that kind of file. The shell takes a much different approach. What command you use on a file depends entirely on what you want to accomplish.

cat

cat has nothing to do with cats. It's short for catenate, and it dumps the contents of text files.  Cats think about lots of things, but not usually about text files.

For example, here's one that contains basic information about the system's processor:

less

Sometimes you'll want to look at a really long file, one that takes up more room than you've got in your terminal.

less is what's known as a pager. It will display one page of a file at a time and let you scroll up and down through the file at your leisure.

/usr/share/dict/american-english is a long list of known words in American English. (If that's not available, try /usr/share/dict/words, which should point to the dictionary for your Pi's locale.) Try opening it in less.

Within less, a ton of different keys are available. The basics are easy to remember, but the online help lists all sorts of advanced features.

space

advances by one page

down and up arrows

j and k

scroll by one line

/

searches for text

q

quits to the shell

h

displays help screen

head and tail

Sometimes you just want a quick look at the beginning or end of a file. This is useful for getting a sense of the contents of very large files, and for seeing the latest additions to things like logs that routinely have new data appended to them.

If you want to see a different number of lines than the default, you can specify:

This guide was first published on Jan 13, 2015. It was last updated on Jan 13, 2015. This page (Looking Inside Files: cat, less, head, and tail) was last updated on Jul 21, 2019.