< How to view text files in Linux - 3.0 >

When you see a weird file on your Linux file system, you may want to know what the file contains. We'll take a look at the cat and less commands that you can use for viewing the contents of a text file.

Author: Nana Långstedt < nana.langstedt at gmail.com >
tuXfile created: 20 December 2001
Last modified: 22 September 2005


contents


back to


< Viewing text files with cat >

Many files on a Linux system are files that are human readable and can be viewed as text. For example, shell scripts are just simple text files, as well as many of the important configuration files that can be edited by hand. This is why Linux provides many ways of editing and viewing text files, but here I'll concentrate on cat and less.

cat is a simple little program that displays the contents of a text file when you give the file name as an argument to it:
$ cat view_this

This is a nice way of viewing short files that fit on your screen, but if the file is so long that its contents cannot be displayed on your screen all at once, you'll end up only staring at the end of the file. Maybe not exactly what you want. In most cases, you'll want to use less instead.


< Using the less command >

less is a program that lets you view text files, like cat does, but if the files are so long that they don't fit on your screen, less automatically paginates the file. You use less by giving the file name as an argument to it:
$ less view_this

When viewing the file, you can use Page Up and Page Down keys to move through the file, and typing q will exit.

You can also open several files at the same time so you can navigate from one file to next without closing it first. If you want to open several files, just give all the file names at once:
$ less file1 file2 file3

When viewing several files at the same time, you can use :n for examining the next file and :p for the previous file.

Here are some, but absolutely not all, of the commands you can use in less:

Command / key Action
e, j, Down, or Enter move forward one line
y, k, or Up move backward one line
f, Space, or Page Down move forward one page
b, or Page Up move backward one page
/characters search forward in the file for lines containing the specified characters
n repeat the previous search
:e file_name examine a new file
:n examine the next file
:p examine the previous file
h, or ? display help
q quit

< What next? >

Now you know how to view the contents of text files on Linux. Next, you might want to learn how to copy, move, and delete files at the Linux command line.

Related tuXfiles


Linux help > Working with files > How to view text files in Linux


Copyright © 2001 - 2011 Nana Långstedt