- How to Display Contents of a File in Linux
- How to Display Contents of a File in Linux
- Cat
- Head
- Tail
- Flags
- More
- Less
- Tac
- Grep
- Conclusion
- Related Articles:
- About the Author: Dwight Kraft
- Terminal Basics #5: View the File Contents in Linux
- Use cat command to display file content
- Using the less command to read large text files
- Head and tail to show part of text files
- Practice examples
- Test your knowledge
- How to list the first or last 10 lines from a file without decompressing it in linux [closed]
- 2 Answers 2
How to Display Contents of a File in Linux
Whether you are a regular user or an experienced system administrator, sooner or later, you will need to interact with files in Linux through the command line. For example, you might need to troubleshoot an issue by checking the log files, viewing your system’s details, or even customizing it by editing the configuration files.
Knowing how to display the contents of a file in Linux can make your life easier and save you time from constantly opening text editors. Many built-in functions make viewing files easy, fast, and tailored to your needs.
How to Display Contents of a File in Linux
Cat
The simplest way to view text files in Linux is the cat command. It displays the complete contents in the command line without using inputs to scroll through it.
Here is an example of using the cat command to view the Linux version by displaying the contents of the /proc/version file.
Head
Sometimes the information needed is in the first lines of a file. In that case, use the head command to view the first ten lines of a file in Linux. For example, users can display basic information about the CPU used by viewing the beginning of the /proc/cpuinfo file.
Like the tail command, use the -n flag with the head command to display the desired number of lines, starting from the beginning of a given file. For example, head -5 shows the first five lines of a given file.
Tail
While the cat command is helpful when dealing with a small file, it is not the best way to view large log files. The tail command allows viewing the last ten lines of a file by default instead of filling your terminal window with a wall of text, making it the perfect command to use if you want to check the last log entries.
Here is the output of the tail command.
Flags
A user can select how many lines the command should display by passing the —n flag (where n is an integer). For example, the tail -15 command will output the last 15 lines on a given file.
Another helpful flag used with the tail command is -f. It outputs the last ten lines of a file by default, but it also keeps displaying new entries as the file is updated. This function is beneficial when viewing the latest updates in log files to troubleshoot an issue. If you only want to use this functionality, you can use the tail -f command, and it will only display the entries that appeared in the file after running the command.
More
Another way to view file contents in Linux is the more command. It displays a file in the terminal, one page at a time. While using the more command, the Enter key scrolls through the file line by line, or the Space key scrolls one full screen at a time. Finally, you can close the file by pressing the Q key.
Here is an example of using more to display the contents of the cpuinfo file in Linux.
Less
While more is a handy command, it does come with a drawback. After closing a file, its contents stay written in the terminal window, filling it with text, forcing users to either clear the window or scroll back up to find something. It can also be slow, as it loads the entire file though displaying only one page at a time.
That’s where the less command comes in handy. It’s very similar to more, but with the benefit of not keeping all the text in the terminal window. The less command also comes with a built-in search function, allowing you to highlight the parts of the file for which you are looking. To search with less, press the forward slash key followed by the text you want to search.
The following is what the search function looks like if we search for /cpu in the cpuinfo file.
Tac
Another exciting way to display the contents of a file in Linux is in reverse order. To do so, use the tac command. It is similar to cat but reversed, reading and displaying the file starting from the last line. For example, here is what the output of tac looks like used to display the contents of the cpuinfo file.
For better readability, pipe the tac command into less to scroll through the file. Users accomplish piping using the desired command, the pipe character, and the other command. The syntax is as follows.
Here is an example output from this command.
Grep
While not used for displaying the contents of a file, the grep command is handy for filtering the output of commands. For example, grep works for searching for specific text in a file.
In addition, pipe the output of other commands through grep, narrowing the search to what we are looking for in the file.
Here is an example of piping the output from the head command into the grep command.
Conclusion
While it might seem intimidating at first, learning how to display the contents of a file in Linux will make it significantly easier to navigate through any Linux distribution. These skills save time and make your job easier.
Do you already know how to display the contents of a file in Linux and just need hosting? Liquid Web has managed hosting options to help you get started with your next project. Contact our sales team to make your purchase.
Related Articles:
About the Author: Dwight Kraft
Dwight is a 3rd year IT student that is passionate about computer hardware and pretty much everything electronics-related. In his free time, he builds PCs and plays guitar for fun.
Join our mailing list to receive news, tips, strategies, and inspiration you need to grow your business
Terminal Basics #5: View the File Contents in Linux
In this chapter of the Terminal Basics series, you’ll learn about viewing the contents of files in the Linux command line.
You learned to create new files in the previous chapter of the Terminal Basics series. In this chapter, you’ll learn to read the files. I’ll be discussing the most common Linux commands to display the contents of a text file. Before you do that, let’s create our ‘playground’ with sample files. Let’s create a directory first and switch to it.
mkdir display_files && cd display_files
And then, create a new file named columbo.txt with the following text (use the cat command with >> as discussed in the previous chapter):
Prescription: Murder Ransom for a Dead Man Murder by the Book Death Lends a Hand Dead Weight Suitable for Framing Lady in Waiting Short Fuse Blueprint for Murder
You don’t have to type it all by yourself. You can copy-paste in the terminal using Ctrl+Shift+V. Most terminals support this shortcut. With things set, let’s see various ways of viewing files in the Linux terminal.
Use cat command to display file content
The cat command is the most popular method to view files in Linux. It is dead simple to use. Just give it the file name and it displays the file content on the screen. Things cannot go simpler than this.
This is the output it shows:
Optional challenge: Use the cat or echo command with >> redirection to add a new line with «Etude in Black» text to the columbo.txt file. Refer to the previous chapter if you need help.
Using the less command to read large text files
The cat command is so simple. In fact, it is too simple. And simple doesn’t work in complicated scenarios. Try using the cat command to view the content of the services file.
This services is a huge file with hundreds of lines. When you use cat, it floods the entire screen with the entire text. This is not ideal. Can you read the first line of the file? Yes, you can but you have to scroll all the way up. If the file has thousands of lines, you won’t even be able to scroll back to the first few lines. This is where the less command comes into the picture. It lets you read the contents of a file in a page-by-page manner. You exit the viewing mode and your terminal screen is clean as ever. Use the less command to read the services file:
Now you are in a different viewing mode. You can use the arrow keys to move line by line. You can also use the Page Up and Page Down keys to move up and down by pages. You can even search for certain text using /search_term. When you are done reading the file, press Q key to exit the less view and go back to the normal terminal viewing. This table will help you use less:
Keys | Action |
---|---|
Up arrow | Move one line up |
Down arrow | Move one line down |
Space or PgDn | Move one page down |
b or PgUp | Move one page up |
g | Move to the beginning of the file |
G | Move to the end of the file |
ng | Move to the nth line |
/pattern | Search for pattern and use n to move to next match |
q | Exit less |
From viewing files in real time to bookmarking text, less can do a lot more. Read this to learn more about it.
Head and tail to show part of text files
If you only want to see certain parts of the text file in cat-styled display, use the head and tail commands. By default, the head command displays the first 10 lines of a file.
Practice examples
#create or clear the content of the file echo -n > sample #put content to the file for i in do echo "This is the line $i" >> sample done
Create a new file named script.sh and copy-paste the above script content into it. Now run the script like this to generate your sample file:
Now, you have got a file named sample that contains lines like «This is the line number N» for every 70 lines.
Let’s take it to the next level. You can combine them both to show specific lines of a file. For example, to show lines from 35 to 40, use it like this:
head -n 40 filename | tail -n +35
Here:
- head -n 40 filename will display the first 40 lines of the file.
- tail -n +35 will display the lines from the 35th line to the end of the output from the head command. Yeah! Mind the + sign that changes the normal behavior of the tail command.
You can also combine them to show only a particular line. Let’s say you want to display the 55th line; combine head and tail like this.
head -n 55 filename | tail -n 1
- head -n 55 filename will display the first 55 lines of the file.
- tail -n 1 will display the last line of the output from the head command, which will be the 55th line of the file.
Test your knowledge
Time for you to exercise your grey cells and practice what you learned in this chapter.
- Use the same sample file and display lines from 63 and 68.
- Now display the lines from 67 to 70.
- How about displaying the first line only?
- What do you see in the /etc/passwd file? Display its content.
That’s it for this chapter. Next, you’ll learn about removing files and folders in the command line. Stay tuned.
How to list the first or last 10 lines from a file without decompressing it in linux [closed]
I have a .bz2 file. I want to list the first or last 10 lines without decompress it as it is too big. I tried the head -10 or tail -10 but I see gibberish. I also need to compare two compressed file to check if they are similar or not. how to achieve this without decompressing the files ? EDIT: Similar means identical (have the same content).
What do you mean by similar? BZ2 is a block-based format, so it is possible to decompress only small chunks of a file without reading the whole.
For comparing two compressed files, you may find something on this page, although the question does specifically asks for .zip files. stackoverflow.com/questions/587442/…
The files must get decompressed. I think what you’re actually asking is «without having to save a copy of the decompressed file.»
2 Answers 2
While bzip2 is a block-based compression algorithm, so in theory you could just find the particular blocks you want to decompress, this would be complicated (e.g. what if the last ten lines you ultimately want to see actually spans two or more compressed blocks?).
To answer your immediate question, you can do this, which does actually decompress the entire file, so is in a sense wasteful, but it doesn’t try to store that file anywhere, so you don’t run into storage capacity issues:
bzcat file.bz2 | head -10 bzcat file.bz2 | tail -10
If your distribution doesn’t include bzcat (which would be a bit unusual in my experience), bzcat is equivalent to bzip2 -d -c .
However, if your ultimate goal is to compare two compressed files (that may have been compressed at different levels, and so comparing the actual compressed files directly doesn’t work), you can do this (assuming bash as your shell):
This will decompress both files and compare the uncompressed data byte-by-byte without ever storing either of the decompressed files anywhere.