Which Command Should You Use To Display The Content Of A File?

Which Command Should You Use To Display The Content Of A File?

Exploring the contents of a file is essential for extracting information and understanding its context. In this guide, we’ll delve into the top five methods that allow you to effectively view and access the content within various types of files. Whether you’re dealing with documents, images, or data files, these techniques will empower you to uncover the valuable information they hold.

In the Linux terminal, almost any file you work with can be treated as text. This includes configuration, log, and script files. Learning to swiftly access file content in the terminal saves time and enriches your system understanding. This guide will teach you how to seamlessly view these files in Linux’s terminal environment.

1. Cat

The cat command in Linux is a handy way to view file contents. It can swiftly display content in the terminal or concatenate outputs.

here’s an example of how the cat command is used in Linux:

Let’s say you have a file named “sample.txt” with the following content:

Hello, this is a sample text file.
It contains some lines for demonstration.
Feel free to explore and learn!

You can view the contents of the file “sample.txt” using the cat command as follows:

cat sample.txt

This command will display the content of the “sample.txt” file in the terminal:

Hello, this is a sample text file.
It contains some lines for demonstration.
Feel free to explore and learn!

Remember that the cat command is commonly used to view the contents of text files in the terminal.

2. nl

To view line numbers alongside content in a file, the nl command is your solution. Similar to the cat command, you use it in the same manner, with the distinction that nl automatically includes line numbers.

here’s an example of how the nl command is used in Linux:

Let’s consider a file named “poem.txt” with the following content:

The road not taken
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;

You can use the nl command to view the contents of the file with line numbers:

nl poem.txt

This command will display the content of the “poem.txt” file with line numbers:

 1  The road not taken
 2  Two roads diverged in a yellow wood,
 3  And sorry I could not travel both
 4  And be one traveler, long I stood
 5  And looked down one as far as I could
 6  To where it bent in the undergrowth;

The nl command is used to display the contents of a file along with line numbers.

For left-justified line numbers, utilize the -nln option with nl command. Explore more options in the manual using “man nl”.

3. More

Until now, you’ve likely used your mouse scroll wheel or keyboard shortcuts like Shift+Page Up/Down to navigate output. However, the ‘more’ command offers a simpler method for viewing lengthy files that exceed the terminal’s window size.

here’s an example of how the more command is used in Linux:

Let’s say you have a file named “longtext.txt” with lengthy content:

This is a long text file used for demonstration purposes.
It contains several lines to showcase the 'more' command.
You can use the 'more' command to navigate through long files
one screenful at a time. This makes it easier to read and manage
large amounts of text without overwhelming the terminal.

You can use the more command to view the contents of the “longtext.txt” file:

more longtext.txt

When you execute this command, the content of the file will be displayed one screenful at a time. You can use the Space key to navigate to the next screen, and the B key to return to the previous screen. The more command helps you read and manage large files comfortably in the terminal. Within the ‘more’ command’s viewing window, navigation can occur either line by line or page by page. To move through one line at a time, use Enter; for one page at a time, utilize the Space key. To return to the previous page, press the B key. Exiting the ‘more’ command’s viewing window is as easy as pressing q. This action promptly returns you to the terminal screen. Thus, ‘more’ commands enable seamless viewing of lengthy configuration or log files without disrupting your terminal workflow.

4. Less

Less represents a contemporary evolution of the ‘more’ command. In earlier Linux days, ‘more’ lacked upward scrolling functionality. When ‘less’ emerged, it addressed this limitation.In addition to bidirectional scrolling, ‘less’ offers horizontal scrolling, improves binary file management, and facilitates text searches.

Let’s look at our simple-list.txt file through less:

here’s an example of how the less command is used in Linux:

Let’s consider a file named “article.txt” with a significant amount of content:

In recent years, artificial intelligence (AI) has rapidly
advanced and found applications in various industries.
From self-driving cars to medical diagnoses, AI is
revolutionizing the way we live and work.

You can use the less command to view the contents of the “article.txt” file:

less article.txt

When you execute this command, the content of the file will be displayed one screenful at a time. You can use the Space key to navigate to the next screen, the B key to go back to the previous screen, and the Q key to exit the less viewer.The less command allows you to read and navigate through large files conveniently in the terminal.The arrow keys on the keyboard are used to navigate, in addition to the navigation keys for more commands. While you are in the viewing window of less, you can use /word to search through the file contents for word.The ‘less’ command goes beyond advanced capabilities, like viewing file contents at the initial occurrence of a specific word.

5. Head

If you just want to quickly view the first ten lines of a file, you can do that through head command as shown below:

here’s an example of how the head command is used in Linux:

Let’s assume you have a file named “numbers.txt” with the following content:

1
2
3
4
5
6
7
8
9
10

You can use the head command to display the first few lines of the “numbers.txt” file. For instance, to display the first 5 lines:

head -n 5 numbers.txt

This command will output:

1
2
3
4
5

The head the command allows you to preview the beginning of a file, which can be especially useful for checking file contents without displaying the entire file. By default, the terminal displays only the initial ten lines. Similarly, the -c option can be used to print a specific number of bytes from the file to the terminal.

6. Tail

The tail the command works like the head command, with the only major difference being that it shows the last ten lines of the file instead of the first ten lines.

Certainly, here’s an example of how the tail the command is used in Linux:

Let’s consider a file named “server.log” with the following content:

[2023-08-01 10:15:20] Connection established.
[2023-08-02 14:30:45] Data processing in progress.
[2023-08-03 18:05:02] Server load increasing.
[2023-08-04 21:55:10] Error: Connection lost.
[2023-08-05 08:10:30] Server restarted successfully.

You can use the tail command to display the last few lines of the “server.log” file. For instance, to display the last 3 lines:

tail -n 3 server.log

This command will output:

[2023-08-03 18:05:02] Server load increasing.
[2023-08-04 21:55:10] Error: Connection lost.
[2023-08-05 08:10:30] Server restarted successfully.

The tail command is useful for checking the most recent entries in log files or any situation where you want to view the end of a file. Furthermore, the ‘head’ and ‘tail’ commands can complement other file-viewing commands from the tutorial, enhancing the output for users.For instance, you can utilize the ‘nl’ command to initially display the file with line numbers. Then, you can pipe the result to ‘less’ to view the last three lines of the file, demonstrated below:

FAQ- Which Command Should You Use To Display The Content Of A File?

Q1. Which command is used to view the contents of a file?

Ans. The cat command is the simplest way to view the contents of a file. It displays the contents of the file(s) specified on to the output terminal. Sometimes, we might want to number the lines in the output.

Q2. What is the command to view the contents of a file in Unix?

Ans.For more information about this command, type man more at the Unix system prompt. cat– Displays the contents of a file on your terminal. Result: Displays the contents of the file “new-file” on your terminal

Q3. How do I view the contents of a folder in Linux?

Ans. Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment