Displaying a File

Cat Command

It’s used to read the contents of a file.

Example

Consider the below example.

file.txt

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  • To view the contents of the file, run the below command from your terminal.

Syntax

cat <file_name>

For example, to display the contents of a file named file.txt, type the following.

cat file.txt

More Command

It views the text files in the command prompt, displaying one screen at a time in case the file is large.

Syntax

more filename

To display file.txt, type the following.

more file.txt

While viewing the text file, you can use the following controls.

  • Enter key - to scroll down line by line.

  • Space bar - to go to the next page.

  • b key - to go to back one page.

Less Command

It displays a file, allowing forward/backward movement in it. Some commands are listed below.

Command

Description

/

Searches for a string

return

Scrolls forward one line

space

Scrolls one page

y

Scrolls back one line

b

Scrolls back one page

q

Quit

Head and Tail Commands

head command displays the top part of a text file. tail command displays the bottom part of a text file.

By default, commands head and tail display 10 lines, but adding -n option changes the number of displayed lines.

  • For example,to display the first 3 lines of a file file.txt, follow the below syntax.

Syntax

head -n3 file.txt

Output

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
  • If you would like to print the last 2 lines of file.txt, follow the below syntax.

tail -n2 file.txt

Output

It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Comparing Files

diff command compares the contents of two files and displays the differences. For example, to compare file1.txt and file2.txt, enter the below command.

diff file1.txt file2.txt