Home > Articles

  • Print
  • + Share This

Viewing Text Files

Linux distributions come with a number of text-viewing programs. The following sections discuss several of these programs, such as the cat command and interactive viewers (called pagers), that you can use to view text files without running a text editor.

Viewing Text Files with the cat Command

Use the cat (concatenate) command to print the contents of files to your console display or terminal window. This command is best used to print short files to your display because long files will scroll too fast for you to read.

$ cat /etc/issue 
Red Hat Linux release 5.0 (Hurricane) 
Kernel 2.0.31 on an i586 

Use the cat command's -n (line-numbering) option to have file listings automatically numbered:

$ cat -n /etc/issue 
     1 
     2  Red Hat Linux release 5.0 (Hurricane) 
     3  Kernel 2.0.31 on an i586 
     4 

Display multiple files either by listing the names on the command line or by using a wildcard. Use output redirection operators, such as > or >>, to create, copy, overwrite, or append a single file or multiple files. For example, the following command line combines the contents of file1.txt and file2.txt, and creates file3.txt:

$ cat file1.txt file2.txt >file3.txt

The next line appends the contents of file1.txt onto file2.txt:

$ cat file1.txt >>file2.txt

Finally, the following command line takes the contents of file2.txt and either creates or overwrites file1.txt:

$ cat file2.txt >file1.txt

By redirecting keyboard input to a file, the cat command can be used as a quick text editor:

$ cat > friends.txt 
Michael 
Cynthia 
Lisa 
Mark 

The cat command reads characters from the keyboard until Ctrl+D (the end-of-text character) is typed. The cat command will print any type of file, including non–human-readable files like program binaries. If you accidentally cat a binary file, you can stop cat by using the Ctrl+C key sequence. catting a binary file can have unpredictable results, such as scrambling your terminal characters. For this reason, you should really avoid doing this by using the file command to find out what sort of file it is that you want to cat:

$ file /bin/gunzip 
/bin/gunzip: ELF 32-bit LSB executable, Intel 80386, version 1,  
dynamically linked (uses shared libs), stripped 
$ file bookmarks.html 
bookmarks.html: exported SGML document text 
$ cat bookmarks.html 

For more details about using the cat and file command, see their manual pages.

Viewing Text Files with the less and more Pager Commands

Pager commands such as more and less are used to interactively read text files. Most users will prefer to read files by using the less command, which has more features than the more pager. Both more and less are used with a filename on the command line, and both accept wildcards to read multiple files. Following are a few examples:

$ less friends.txt 
Michael 
Cynthia 
Lisa 
Mark 
friends.txt (END) 

Because the less command is designed to let the user scroll through multiple pages of a file, it stops printing the contents of the file to your display at the end of the screen rather than the end of a file. To move down line by line, press the down-arrow key. You can press the spacebar to move down an entire screen or press b to move back up a screen. To move up line by line, press the up-arrow key. Use P to scroll up screen by screen. To quit, press q. Use / to search for words or strings. See the man page on less for more details.

The less command not only accepts multiple filenames and wildcards, but can also be used with input and output redirection operators. The more and less programs are similar, but the less pager has many additional features, including the following:

  • Using cursor keys to scroll through documents

  • Horizontal scrolling for wide documents

  • Jumping to document bookmarks

  • Keyboard command customization

  • Sophisticated searching of multiple documents

The more command will automatically quit when it reaches the end of the file. (Remember, the less command requires that you type q.) The more command will only allow you to move forward through the file, whereas the less pager allows you to move both forward and backward.

The less pager is usually the default manual page reader, although this option can changed by defining a $PAGER shell environment string. Check out Table 3.2 for some less pager commands.

Table 3.2  Common less Pager Commands

Action

more

less

Forward one line

Enter or s

Enter, e, j, or cursor down

Backward one line

 

y, k, or cursor up

Forward one screen

Space, z, or f

Space, z, or f

Backward one screen

b

b

Help

h or ?

h

Previous file

:p

:p

Next file

:n

:n

Search str

/

/

Quit

q

q


Custom keys, or key bindings, for the less command can be defined and created in a binary file called .less in your home directory. Use the lesskey command to read or create this file. See the lesskey manual page for more information.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.