Chapter 4: Editing and viewing files

Viewing files

face Robert Craven

Let’s now view the contents of the misc.txt file we just edited. One way to do this is to use cat, which simply outputs its contents:

username@MACHINE:notes$ cat misc.txt
Had we but world enough and time,
This coyness, lady, were no crime.
We would sit down, and think which way
To walk, and pass our long love's day.

(And so on, for the rest of the file.)

This command will also take a list of files and output their contents one after the other, as with:

username@MACHINE:notes$ cat file1.txt file2.txt file3.config

Sometimes you don’t want to see all of a file—you might be interested in the start, or the end.

The command head will output only the first few lines of a file—by default this is 10 lines, but with the -n option you can specify how many:

username@MACHINE:notes$ head -n 7 misc.txt
Had we but world enough and time,
This coyness, lady, were no crime.
We would sit down, and think which way
To walk, and pass our long love's day.
Thou by the Indian Ganges' side
Shouldst rubies find; I by the tide
username@MACHINE:notes$

And, as you might guess, tail does the same for the end of a file:

username@MACHINE:notes$ tail -n 2 misc.txt
Thus, though we cannot make our sun
Stand still, yet we will make him run.
username@MACHINE:notes$

None of these three commands—cat, head, and tail—are interactive: they output parts of the contents, and then return you to the prompt.

To scroll back and forth in a file, use less. This doesn’t let you edit a file, but it will allow you to scroll up and down, using and .