Shell and DoC Systems, Lab 1
The Linux Terminal and Scientia
Chapter 4: Editing and viewing files
Viewing files
Let’s now view the contents of the mynotes.txt
file we just edited.
One way to do this is to use cat
, which simply outputs its contents:
username@MACHINE:misc$ cat mynotes.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:misc$ 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:misc$ head -n 7 mynotes.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:misc$
And, as you might guess, tail
does the same for the end of a file:
username@MACHINE:misc$ tail -n 2 mynotes.txt
Thus, though we cannot make our sun
Stand still, yet we will make him run.
username@MACHINE:misc$
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 ↓.