View files using – cat, more, tail, head and wc commands

There are several commands that display information about a file in the read-only format. The file-viewing commands include the following:

  • cat
  • more
  • tail
  • head
  • wc

cat Command

The cat command displays the content of one or more text files on the screen without pausing.

$ cat filename

For Example:

# cat data.txt
northwest       NW      Joel Craig          10
western         WE      Sharon Kelly        40
southwest       SW      Chris Foster        33
central         CT      Sheri Watson        44

Do not use the cat command to read binary files. Using the cat command to read binary files can cause a terminal window to freeze. If your terminal window freezes, close the terminal window, and open a new terminal window.

Note: Before you attempt to open a file with the cat command, it is recommended that you first run the file command to determine the file type.

more Command

The more command displays the content of a text file one screen at a time.

$ more filename

The

--More--(n%)

message appears at the bottom of each screen, where n% is the percentage of the file that has been displayed. When the entire file has been displayed, the shell prompt appears.

When the –More–(n%)prompt appears at the bottom of the screen, you can use the keys described in the table to scroll through the file.

Keyboard Command Action
Space bar Moves forward one screen
Return Scrolls one line at a time
b Moves back one screen
h Displays a help menu of features
/string Searches forward for pattern
n Finds the next occurrence of pattern
q Quits and returns to the shell prompt

head Command

The head command displays the first 10 lines of a file.

$ head -n filename

You can change the number of lines displayed by using the -n option. For example, to display the first five lines of the /var/log/messages file, enter the head command with the -n option set to 5.

$ head -5 /var/log/messages

tail Command

The tail command displays the last 10 lines of a file.

$ tail –n/+n filename

You can change the number of lines displayed by using the -n or +n options.
– The -n option displays n lines from the end of the file.
– The +n option displays the file from line n to the end of the file.

For example, to display the last four lines of the /var/log/messages file, enter the tail command with the -n option set to 4.

$ tail -4 /usr/dict/words

For example, to display line 10 through the end of the data.txt file, enter the tail command with the +n option set to 10.

$ tail +10 data.txt

wc Command

The wc command displays the number of lines, words, and characters contained in a file.

$ wc -options filename

You can use the following options with the wc command.

Symbol Description
-l Line count
-w Word count
-c Byte count
-m Character count

When you use the wc command without options, the output displays the number of lines, words, and characters contained in the file. For example, to display the number of lines, words, and characters in the dante file, use the wc command.

$ wc data.txt
32      223     1319    data.txt

For example, to display the number of lines in the dante file, enter the wc command with the -l option.

$ wc -l data.txt
32 data.txt
Related Post