Beginner Linux users are often limited to the terminal. Viewing a file in the command line is a different idea to them, and it is strikingly different from viewing a file on a notepad in Windows. However, it is possible for the Linux terminal to display the contents of the file for you. This is a comprehensive guide about methods to display the contents of a file in Linux.
Why Should You Learn File Viewing in Linux/Ubuntu?
No matter if you are a system admin or regular user, you will interact with files in Linux through the command line. Probably, you would have to check the log files for troubleshooting as an administrator. Sometimes, admins also customize the configuration files. A regular user may also have to view system details at any point.
How to Display Content of Files in Linux/Ubuntu?
There are different commands in Linux to view the file content:
- Using cat Command
- Using nl Command
- Using Head Command
- Using Tail Command
Method 1: The Cat Command
Cat is the most famous and the most simple command to view file content in Linux. For this purpose, all you need to do is write cat then the filename, and you will see all the content of the selected file on your screen. However, it would not be a very good option when your file has 2000 lines of text.
$ sudo cat alph.txt
Here, “alph.txt” represents the name of the targeted file. You can replace it with any valid file name of your choice. On successful execution of the cat command the content of the selected file will be shown as follows:
Method 2: The nl Command
The nl command works just like the cat command, but it adds a line number at the start of every line.
$ sudo nl alph.txt
Method 3: The Head Command
Not every time you need to view the complete text of a file. Maybe we just need to view some of the starting parts of the text. The Head command is best for such situations. It only displays the first 10 lines of text.
$ sudo head alph.txt
Method 4: The Tail Command
Similarly, there are situations when the end portion of the file is important for you. It displays the file from the end.
$ sudo tail alph.txt
Conclusion
The commands cat, head, tail, and nl can help you display the content of a file in Linux. All methods have their pros and cons. Users should pick the one that best suits their personal requirements. That may seem a little difficult at the start but becomes very easy after some hours of practice. This blog post has discussed several methods of displaying the content of a file in Linux/Ubuntu.
Frequently Asked Questions
How do I display the contents of a file in Linux using the cat command?
To display the contents of a file in Linux using the cat command, simply use 'cat' followed by the file name. This command will show the entire content of the specified file on the terminal.
What is the purpose of using the nl command to view file content in Linux?
The 'nl' command in Linux is used to display the contents of a file with line numbers added at the beginning of each line. It helps in better readability and reference while viewing the file content.
How can I view the first few lines of a file in Linux using the head command?
To view the first few lines of a file in Linux, you can use the 'head' command. Simply type 'head' followed by the file name to display the initial lines of the file.
What is the purpose of using the tail command to view file content in Linux?
The 'tail' command in Linux is used to display the last few lines of a file. It is helpful when you want to quickly check the end of a file without scrolling through the entire content.
How can I display a specific range of lines from a file in Linux?
To display a specific range of lines from a file in Linux, you can use the 'sed' command with appropriate options. Specify the range of lines you want to display to view only the selected content.
Is it possible to view the content of multiple files simultaneously in Linux?
Yes, you can view the content of multiple files simultaneously in Linux by using the 'cat' command with multiple file names separated by space. This allows you to see the content of all specified files in one go.
How can I display hidden characters or non-printing characters in a file on Linux?
To display hidden characters or non-printing characters in a file on Linux, you can use the 'cat' command with the '-v' option. This will show special characters such as tabs and line breaks in the file.
What is the difference between using 'cat' and 'more' command to view file content in Linux?
While 'cat' displays the entire content of a file at once, the 'more' command allows you to view the content page by page, enabling you to navigate through the file easily. Choose 'cat' for a quick overview and 'more' for detailed reading.