How do I view the contents of a file in Linux using the cat and less commands?

Published on Aug. 22, 2023, 12:20 p.m.

Yes, you can use the cat and less commands to view the contents of a file in Linux.

To view the contents of a file using the cat command, simply type the command cat [filename] where [filename] is the name of the file you want to view. The contents of the file will be displayed on the terminal.

To view the contents of a file using the less command, simply type the command less [filename]. The contents of the file will be displayed in the less pager, which allows you to scroll up and down through the file, search for specific text, and perform other operations. Press the “q” key to quit the less pager and return to the command prompt.

Sure, here are some examples of how to use the cat and less commands in Linux to view the contents of a file:

To view the entirety of a small file using the cat command:

cat example.txt

This will display the contents of the example.txt file on the terminal.

To view the contents of a large file using the less command:

less largefile.log

This will display the contents of a large file called largefile.log in the less pager, which allows you to scroll up and down through the file. Press the “q” key to quit the less pager and return to the command prompt.

To combine the contents of two or more files using the cat command:

cat file1.txt file2.txt > combined.txt

This will combine the contents of file1.txt and file2.txt and save them to a new file called combined.txt.

To display the contents of a file with line numbers using the cat command:

cat -n example.txt

This will display the contents of example.txt with line numbers on the left-hand side of the terminal.

Tags: