How to view contents of a file in Linux

You can use any of the method given below to view contents of a file in Linux. Let us say the file name is “filename”. Then you can view its content…

1. using cat command

chankey@linuxstall:$ cat filename

or

chankey@linuxstall:$ cat < filename

2. using head command

chankey@linuxstall:$ head filename

3. using tail command

chankey@linuxstall:$ tail filename

4. using pg command

chankey@linuxstall:$ pg filename

5. using more command

chankey@linuxstall:$ more filename

6. using paste command

chankey@linuxstall:$ paste filename

7. using grep command

chankey@linuxstall:$ grep '.*' filename

8. using cut command

chankey@linuxstall:$ cut -c 1- filename

9. using while loop

chankey@linuxstall:$ while read line
>do
>echo $line
>done < filename

10. using xargs command

chankey@linuxstall:$ xargs -L1 < filename

11. using sed

chankey@linuxstall:$ sed '' filename

or

chankey@linuxstall:$ sed -n 'p' filename

or

chankey@linuxstall:$ sed -n '1, $p' filename

12. using awk

chankey@linuxstall:$ awk '1' filename

or

chankey@linuxstall:$ awk '{print;}' filename

or

chankey@linuxstall:$ awk '{print $0;}' filename

13. using perl

chankey@linuxstall:$ perl -pne '' filename

or

chankey@linuxstall:$ perl -ne 'print;' filename

or

chankey@linuxstall:$ perl -ne 'print $_;' filename

If you know some more ways then share them in comments.

About the author

Chankey Pathak is the founder of Linux Stall. He is a Perl developer at Wokana Technologies. He is a Linux and Perl enthusiast. Check out his latest website on Tech News. You may follow him on Google+.