next up previous contents index
Next: Editors Up: Viewing and Editing Files Previous: Viewing and Editing Files   Contents   Index

Text Viewers

To view the contents of a file you can use the commands cat, head, tail, more, and less. These commands are pretty easy to use, so we will do a few examples. There are two dictionary files in the /usr/share/dict directory called linux.words and words. We are going to take a look at these two files using the viewing commands. First cat:

bash$ cat /usr/share/dict/linux.words

I would put the results, but that would require quite a few lines. Well, head is next:

bash$ head /usr/share/dict/linux.words
Aarhus
Aaron
Ababa
aback
abaft
abandon
abandoned
abandoning
abandonment
abandons

This one was not as long as the last one. The head command prints the first 10 lines of the file to the screen by default. If you only wanted the first three lines you could type this:

bash$ head -n 3 /usr/share/dict/linux.words
Aarhus
Aaron
Ababa

You can also do the same with tail for the end of the file:

bash$ tail /usr/share/dict/linux.words
zoologically
zoom
zooms
zoos
Zorn
Zoroaster
Zoroastrian
Zulu
Zulus
Zurich

Like head with tail you can say the number of lines you would like to display:

bash$ tail -n 3 /usr/share/dict/linux.words
Zulu
Zulus
Zurich

This is good when you would like to look at a log file or something else that is long, but you only want to see the last few lines of the file. The commands more and less are very similar. You start more the same way as cat, head, and tail:

bash$ more /usr/share/dict/linux.words

This will display one screen of text at a time. With more you can press the spacebar to scroll down a screen at a time and the Enter key to go down a line at a time. The biggest problem with more is that you cannot scroll back up. This is where less is very strong:

bash$ less /usr/share/dict/linux.words

This will also display a screen of text at a time. The up and down arrows go up and down a line at a time. You can also press Page Up and Page Down to scroll a page at a time. With both more and less you can press q to quit early. Output of programs can be piped into these programs also. This is how you would pipe the output of the ls command into less:

bash$ ls /dev/ | less

This would of course put a lot of text on the screen so we will not put it here. With these commands you can see many text files better then you would have been able to before.


next up previous contents index
Next: Editors Up: Viewing and Editing Files Previous: Viewing and Editing Files   Contents   Index
Joseph Colton 2002-09-24