How to use the `cat` command to display the contents of a file?

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

Here’s the answer to your question:
To display the contents of a file using the cat command in Linux, simply provide the filename as an argument to the command. For example, if you have a file called example.txt, you can display its contents with the following command:

cat example.txt

This will display the contents of the example.txt file on the terminal. If the file is large, you can use the less command to view the contents page by page or the tail command to view the last few lines of the file.

To use the cat command to display the contents of a file with a limited number of lines, you can pipe the output of the cat command to the head command using the -n option to specify the number of lines you want to display. Here’s an example:

cat example.txt | head -n 10

This will display the first 10 lines of the example.txt file. You can adjust the number after the -n option to display more or fewer lines as desired.

Alternatively, you can use the less command to view the file one page at a time and limit the number of lines displayed per page. To do this, simply pipe the output of the cat command to less, like this:

cat example.txt | less

Then you can use the space bar to advance by one page and the arrow keys to navigate up and down within a page.

Tags:

cat