Viewing text files: cat, head, tail, more, less



If you're new here, you may want to subscribe to my RSS feed.

Many Unix files are text files. This may seem simplistic, but it’s actually very powerful. Text files are easy to create, read, and manipulate. Many Unix commands are for working with text files.

The first category of Unix commands for working with text files might be called “text viewers”. These includes commands such as cat, head, tail, more, and less. All of these commands are used by typing the command plus the name of a file.

cat — Short for “concatenate”, this command is used to temporarily combine files. But if you use it on only one file, it just displays the file to the screen.

head — This command shows the first 10 lines of a file. (You can adjust the number of lines using the flag -n.)

tail — This command shows the last 10 lines of a file. (You can also use -n here to show more lines.)

more — Like cat, this shows the entire contents of a file, but prompts you for “more?” between each page. Hit the space bar to go to the next page, or use the up and down arrows. Hit “q” to quit.

less — This is another “pager” like more, i.e. it shows one page at a time. It was meant to be a successor or alternative to more and was hence given the opposite name less. It had more features than more (sounds like an Abbott and Costello routine) when it first came out, but now the two commands are about the same.

Here are some examples:

How to view your hosts file, where you can map host names to IP addresses:
cat /etc/hosts

If you have Personal Web Sharing turned on, how to view the last 10 lines of the Apache log file:
tail /var/log/httpd/access_log

How to read the Apache configuration file, one page at a time:
more /etc/httpd/httpd.conf

3 Responses to “Viewing text files: cat, head, tail, more, less”

  1. FreeMacUnix » Blog Archive » Pipes Says:

    […] I mentioned previously that Unix uses lots of text files, which are easy to create and manipulate. Unix has a variety of commands to manipulate text files. The way to move text between these commands is with pipes. The pipe symbol is the vertical bar (|) above your Enter key. […]

  2. Casey Koons Says:

    I just wanted to say what a fan I am of this series, and express that if it were to come to life again, you’d have an avid reader.

    -Casey

  3. Janssen Says:

    I’ll second what Casey said.

Leave a Reply