Home –  How to – How to view contents of a file in Linux

How to view contents of a file in Linux

view 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.

3 thoughts on “How to view contents of a file in Linux

  1. fubofo

    Why so many? Why not just simplify it?

  2. Magne Sandøy

    You can also use: less filename
    man less for more info.

  3. dorin

    php -r ‘$f=fopen(“filename”,”r”);echo fread($f,filesize(“filename”));fclose($f);’

Leave a Reply

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