7 Commands to read or view the contents of a file using CLI in Linux

Let me show you 7 commands which can be used to view the contents of its file where each command has its own advantage and you can use them as per your requirement.

1. less command

This is most used and opted command for viewing the contents of any file as you get the option to scroll up and down the file as per your convenience.  So in case the file content you are trying to read is huge you can use this command

Syntax

# less (filename)
# less http.conf (Use up and down arrow to scroll respectively)

 

2. more command

This command can also be used to views files with huge contents but things are a bit complicated as compared to less command. To scroll up and down the page you need to memorize specific buttons.
Syntax

# more (filename)
# more http.conf

SPACE : To scroll down
d : To scroll to the very next line
b : To scroll back to the previous line
 

3. tail command

This command if used without any additional switches displays the last 10 lines of the file you are trying to view. This command can be helpful in case you are specific to view particular content of any file.
Syntax

# tail (filename)
# tail http.conf
# tail -20 http.conf
This will list last 20 lines of http.conf

 

4. head command

This command is similar to tail command with one difference that it shows top 10 lines of any file if used without any switches.

Synatx

# head (filename)
# head http.conf
# head -20 http.conf
This will list first 20 lines of http.conf

 

5. vi or vim command

This command is basically used to edit files but again you can't ignore the fact can it can also be used to view files and close it without editing.
Synatx

# vi (filename)# vi http.conf

Use up and down arrow to scroll up and down. For more information on extra switches use man command

# man vi

 

6. pr command

This command can also be used to view the contents of any file but its not much user friendly. Alternatively you can utilize this using pipe and less command.
Syntax

# pr (filename) | less
# pr http.conf | less

 

7. cat command

This command can also be used to read a file but it has a demerit, you won't get any option to scroll up and down if the file content is very huge. It can be useful if the file you are trying to view has less contents or else you might have to use less command along with pipe as shown above.
Syntax

# cat (filename)
# cat http.conf | less (In case the file content is more)