If you have a text file in Linux that you want to view but you don’t need to make changes to it, you can use the cat and less commands.

Viewing Short Text Files with the cat Command

If you have a short file you want to view that will fit on the screen, use the cat command as follows:

$ cat <filename>

The cat command is good for files that are not longer than the display area of the current Terminal window, and, therefore, do not require scrolling.

Using the cat command

Using the less Command to View Longer Files

If the file is so long that the contents cannot all be displayed in your Terminal window at once, use the less command.

$ less <filename>

It works like the cat command; however, if the file is longer than the Terminal window less automatically paginates the file.

Using the less command

When viewing the file, use the Page Up and Page Down keys to move through the file. To close the file, type q.

You can open several files at the same time and navigate among the files without having to close one and open the next file. Just give less the names of all the files you want to open.

$ less <filename1> <filename2> <filename3>

To move from one file to the next file, press :n. To go to the previous file, press :p.

NOTE: When you open multiple files, typing q closes all files at once.

The following table lists some of the common commands available in less.

Command / KeyAction
e, j, Down, or EnterMove forward one line
y, k, or UpMove backward one line
f, Space, or Page DownMove forward one page
b, or Page UpMove backward one page
/charactersSearch forward in the file for lines containing the specified characters
nRepeat the previous search
:e filenameExamine a new file
:nExamine the next file
:pExamine the previous file
h or ?Display help
qQuit

Remember that cat and less do not allow you to edit files, only view their contents. There are text editors available, such as gedit, for editing text files.

by Lori Kaufman