Linux and Unix Commands to Display Contents of a File

Here are some Linux and Unix system's commands with examples to display the contents of a file.

Displaying Contents of a File Using Cat

There are times when you may want to view the contents of a file but don't want to go to the fuss of running a text editor. Linux/Unix provides some relevant commands, the simplest being cat.

To merely display the contents of a file containing ASCII text, use the cat (short for concatenate) command. The following is an example:

$ cat load_xml.ctl



In this instance, the screen is the default output for the cat command: This means that the results of your command are displayed on your screen unless you specify otherwise.

However, this means you can direct the output of the cat command to other outputs, such as files (this is how LINUX/UNIX manages to print files). For example, you can send the output of the cat command to another file (creating a straightforward backup procedure for single files):

$ cat load_xml.ctl > load_xml.ctl.bak

The system creates the file load_xml.ctl.bak if it doesn't exist. If it does exist, the cat will overwrite the file with the new data once again; we see that UNIX will trample existing files unless you, the user, take some safeguards.

Viewing Files with PG Command

The cat command may be adequate for viewing short files, but it is awkward for viewing long files. The pg command allows you to view files one page at a time. In many ways, pg invokes a primitive text editor not quite as advanced as something like vi or emacs, but powerful enough for rudimentary work.

PG Command Example

$ pg my_file.txt

This displays the first page of the file my_file.txt on your screen, then prompts you for a subsequent command:

  • To display the next page, hit the Enter key.
  • To move back one page, type - (the hidden key).
  • To move ahead or back a given number of pages, use plus (+) or minus (-) and the number of pages to be moved.
  • To move by lines, use plus (+) or minus (-) in combination with 1 (ell) the command +61 moves the text ahead six lines.
  • To move ahead one-half of a page, type d.
  • To search for a string of text, bracket the string between slashes: /your_text/

Using More to Display File Contents

In addition to pg, there's a similar command called more. More presents a page at a time of a file. Some UNIX users argue that more is easier to use than pg, and they may be right; if you find yourself needing the capability to view files often, you will want to compare the two and make your own decision.

Using more is pretty simple: You press the SpaceBar to go ahead one page, and you press q to quit. The following is an example:

$ more my_long_file.txt

Using Head and Tail Commands to View a File

You may not want to scroll through an entire document to get to the end, or you may just want to check out the beginning of a file quickly. Use the head and tail commands in these instances. To view the first 10 lines of the file my_file.txt, use the following:

$ head my_file.txt

To view the last 10 lines of the file my_file.txt, use the following:

$ tail my_file.txt

To view a specific number of lines at the beginning or end of a file, combine head or tail with a numerical argument. The following displays the final 15 lines in the file my_file.txt.

$ tail -15 my_file.txt

Leave a Comment