How to Check File Content on Linux

In this tutorial, you will learn how to check the content of a file on Linux with various commands.

cat Command

Example 1. You can see all contents of a file by using the cat command.

$ cat file_name

Example 2. If you want to see the number of rows in a file, you can use the -n option.

$ cat -n file_name

Example 3. To print the lines containing a specified word, use the grep command in conjunction with the cat command.

$ cat file_name | grep key_word

less Command

The less command only shows the contents of the file that can fit on your screen, so it’s often better to use than the cat command.

$ less file_name

To continue to the next page of your file, press Space. To exit from reading the file, press q.

head Command

If you only need to see the first few lines of a file, the head command is very useful.

Example 1. By default, head will display the first 10 lines of a file.

$ head file_name

Example 2. To display a different number of lines, use the -n option. For example, to display the first 20 lines of a file:

$ head -n 20 file_name

tail Command

Example 1. The tail command will display the last 10 lines of a file:

$ tail file_name

Example 2. We can also specify how many lines we want to see at the end of a file with the -n option. To see the last 20 lines of a file with tail:

$ tail -n 20 file_name

Leave a Comment

Your email address will not be published. Required fields are marked *