previous contents up next

Unix for Advanced Users

6. Manipulating Files

6.2. Viewing Files and File Parameters

6.2.2. It's the head or tail of a cat, more or less

There are a number of simple commands designed to show some part of a a file. Though their functions overlap somewhat, each is useful in its own right.

6.2.2.1. cat

cat stands for concatenate, a verb that literally means to chain [things] together. The command does just that. Given a list of files, it prints them out in order. It can be used along with I/O redirection to combine several files into one file:
cat file1 file2 file3 > file4
cat can also be used in pipelines:
cat file1 file2 file3 | more

6.2.2.2. more

more is an interactive command that display its input one page at a time. You can use it to read multi-screen input that would otherwise scroll off of the screen too quickly.

more has many options, allowing you, for instance, to read several files in a row. The most useful application of more, though, is to read one large piece of input, exactly as in the above example:

cat file1 file2 file3 | more

You can also simply more file1.

6.2.2.3. less

less is a more functional clone of more provided by the GNU project (and named in that project's punning tradition). One added feature of less is that it allows you to scroll back and forth in a file one line at a time with the arrow keys, rather than with alphabetical keys like h, j, k, and l. less reads files incrementally, rather than loading each file into memory like more, so less uses less memory.

6.2.2.4. head

Sometimes you want to read only the first part of a file. This is often true for files produced by the system logger, which can be too large to open quickly in an editor. In this case, the head command can be useful. head takes an argument which specifies the number of lines to print:

head -12 /var/log/messages

If you need to see more lines of text than your terminal can display, more and a pipe can help you:

head -321 /var/mail/myusername | more

6.2.2.5. tail

You might guess that, since head shows the beginning of a file, tail shows the end. You would be right. tail also takes a numerical option that specifies the number of lines to show. A tail -f filename tracks the file, printing out every new line as it is added.

previous contents up next