Top 10 way to display or check the file content in Linux – Do you know that, which is the best way to display content in Linux, there are variety of Linux command can be used to explore the content of file in Linux environment. Let’s try different way below and keep post out our comments to understand your thought.
Let us create a display_demo.txt files which has content below.
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
1. cat command: this is one conmanly used by all Linux user.
Linux command: cat display_demo.txt
vicky@pwr-VirtualBox:~$ cat display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
2. We could use grep command to display whole content.
Linux command – grep ‘.*’ display_demo.txt
vicky@pwr-VirtualBox:~$ grep ‘.*’ display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
3. Awk command is a powerful tool that we can use it for many purposes.
You can also use this awk command to display the whole content from files.
Linux command – awk ‘{print $0;}’ display_demo.txt
Note: passing $0() into awk will by default print all content from file.
vicky@pwr-VirtualBox:~$ awk ‘{print $0;}’ display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
4. Another awk command to display whole content.
Linux command – awk ‘1’ display_demo.txt
Note: passing 1 into awk will by default print all content from file.
vicky@pwr-VirtualBox:~$ awk ‘1’ display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
5. There are another awk command to display whole content which is different from above command.
Linux command – awk ‘{print;}’ display_demo.txt
Note: not putting the $0() in the awk command which will take by default execution.
vicky@pwr-VirtualBox:~$ awk ‘{print;}’ display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
6. There is multiple way to using sed command to display a file’s content.
Linux command – sed ‘ ’ display_demo.txt
Note: without any command in single quote just print the all-file content.
vicky@pwr-VirtualBox:~$ sed ” display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
7. Linux sed command to display the file content, different from above
Linux command – sed -n ‘p’ display_demo.txt
sachin@pwr-VirtualBox:~$ sed -n ‘p’ display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
Note: sed -n option will not allow to do default printing of all content and pointing ‘P’ will be print the content. That will suppress the default printing and print all lines.
8. Below is another variant of sed to display the file content, different from above
Linux command – sed -n ‘1, $p’ display_demo.txt
Note: using the quote and passing the value 1 is tells the program to execute print start from line 1 to till end of file.
vicky@pwr-VirtualBox:~$ sed -n ‘1,$p’ display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
9. There is different way to write down of cat command.
Linux command – cat < display_demo.txt
vicky@pwr-VirtualBox:~$ cat < display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
10. Using paste command, when we used paste command without any option then it will print all lines.
Linux command – paste display_demo.txt
vicky@pwr-VirtualBox:~$ paste display_demo.txt
NO EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER
1 001 Vicky Christophe vicky200@gmail.com 200000200
2 002 Shimmi Singh simmisingh@gmail.com 300000030
3 003 Teena Kumar XXXXXXXXXX@gmail.com XXXXXXXXXX
4 004 Pravina Lobo chXXXXXXXXX@gmail.com 88888xxxx
Thanks for reading out this article till here, if you like this article, i would suggest you to read below article as well.
How to view the status of password in Linux
For understand the full article, you can watch this video.
Title : Top 10 way to display files content in Linux.