- 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
- 5 Commands to View the Content of a File in Linux Command Line
- 5 commands to view files in Linux
- 2. nl
- 3. Less
- 4. Head
- 5. Tail
- Bonus: Strings command
- Conclusion
- How to Use More Command in Linux to Read Large Text Files
- Using more command to read text files in Linux terminal
- Navigating through more
- Display n number of lines per screen
- Skip the first n number of lines
- Search for a string using more command
- Skip multiple blank lines with -s option of more command
- Run more command with help options displayed
- Conclusion
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.
5 Commands to View the Content of a File in Linux Command Line
Here are five commands that let you view the content of a file in Linux terminal.
If you are new to Linux and you are confined to a terminal, you might wonder how to view a file in the command line.
Reading a file in Linux terminal is not the same as opening file in Notepad. Since you are in the command line mode, you should use commands to read file in Linux.
Don’t worry. It’s not at all complicated to display a file in Linux. It’s easy as well essential that you learn how to read files in the line.
Here are five commands that let you view the content of a file in Linux terminal.
5 commands to view files in Linux
Before you how to view a file in Unix like systems, let me clarify that when I am referring to text files here. There are different tools and commands if you want to read binary files.
Cat becomes a powerful command when used with its options. I recommend reading this detailed tutorial on using cat command.
The problem with cat command is that it displays the text on the screen. Imagine if you use cat command with a file that has 2000 lines. Your entire screen will be flooded with the 200 lines and that’s not the ideal situation.
So, what do you do in such a case? Use less command in Linux (explained later).
2. nl
The nl command is almost like the cat command. The only difference is that it prepends line numbers while displaying the text in the terminal.
There are a few options with nl command that allows you to control the numbering. You can check its man page for more details.
3. Less
Less command views the file one page at a time. The best thing is that you exit less (by pressing q), there are no lines displayed on the screen. Your terminal remains clean and pristine.
I strongly recommend learning a few options of the Less command so that you can use it more effectively.
There is also more command which was used in olden days but less command has more friendly features. This is why you might come across the humorous term ‘less is more’.
4. Head
Head command is another way of viewing text file but with a slight difference. The head command displays the first 10 lines of a text file by default.
You can change this behavior by using options with head command but the fundamental principle remains the same: head command starts operating from the head (beginning) of the file.
5. Tail
Tail command in Linux is similar and yet opposite to the head command. While head command displays file from the beginning, the tail command displays file from the end.
By default, tail command displays the last 10 lines of a file.
Head and Tail commands can be combined to display selected lines from a file. You can also use tail command to see the changes made to a file in real time.
Bonus: Strings command
Okay! I promised to show only the commands for viewing text files. And this one deals with both text and binary files.
The Strings command displays the readable text from a binary file.
No, it doesn’t convert binary files into text files. If the binary file consists of actual readable text, the strings command displays those text on your screen. You can use the file command to find the type of a file in Linux.
Conclusion
Some Linux users use Vim to view the text file. Of course, you can easily move from the beginning to the end of the lines and edit the file but it’s overkill for just reading a file. My favorite command to open a file in Linux is the less command. It leaves the screen clear and has several options that makes viewing text file a lot easier.
Since you now know ways to view files, maybe you would be interested in knowing how to edit text files in Linux. Cut and Paste are two such commands that you can use for editing text in Linux terminal. You may also read about creating files in Linux command line.
Which command do you prefer?
How to Use More Command in Linux to Read Large Text Files
Cat command output flooding your terminal screen? Learn to use more command in Linux to view large text files.
When you are new to Linux, you try to use the cat command all the time to read the content of a file. This works great for files with only a few lines out of output, but larger files quickly scroll content past the user making it difficult, or even impossible for you to find what you need.
The cat command is certainly not very practical for viewing large files. You don’t want your entire screen to be filled with the file content.
What you can do here is to use either ‘less command’ or ‘more command’. We’ve already covered the less command on Linux Handbook so I am going to show you how to use more command in this tutorial.
Using more command to read text files in Linux terminal
The more command opens a text file in page views. You can read page by page and when you quit more, there will be no output visible on the screen. Your terminal will be clean and pristine.
Navigating through more
With more you are able to easily scroll through data one page at a time. You can use the following methods for navigation.
Input | Action |
---|---|
Down Arrow / Space Bar | Scroll Down (One Page) |
Up Arrow / B | Scroll Up (One Page) |
Enter | Scroll One Line at a Time |
– number | The Number of Lines per Screenful |
+ number | Display File Beginning from Line Number |
+/ string | Display File Beginning from Search String Match |
q | Quit viewing the text file and return to screen |
While using more, you can enter q to quit at anytime or h to display the built-in help.
Display n number of lines per screen
Typing an -n (where n is an integer) after the command will display only n number of lines as a page.
Skip the first n number of lines
Typing +n (where n is an integer) after the command will skip to that line number before displaying your content.
Search for a string using more command
You can skip to the first instance of a specified string using +/string after our command.
To find the next instance, you can type /string inside the more view to search.
Skip multiple blank lines with -s option of more command
If you have multiple blank lines together, you can trim them into a single blank line with option -s.
Run more command with help options displayed
If you are new to more, you can run it with option -d. This way, it keeps on showing help for navigating.
In addition to that, if you used any illegal character or command in more view, it suggests using help methods using h which displays detailed instructions for navigation or exit.
Conclusion
There are also a few more tricks and tips included that you can look at. Of course, you can always look at the man page of more command for details.
more --help -d display help instead of ringing bell -f count logical rather than screen lines -l suppress pause after form feed -c do not scroll, display text and clean line ends -p do not scroll, clean screen and display text -s squeeze multiple blank lines into one -u suppress underlining // from the detailed instructions page accessible via h while running Display next k lines of text [current screen size] z Display next k lines of text [current screen size]* Display next k lines of text [1]* d or ctrl-D Scroll k lines [current scroll size, initially 11]* q or Q or Exit from more s Skip forward k lines of text [1] f Skip forward k screenfuls of text [1] b or ctrl-B Skip backwards k screenfuls of text [1] ' Go to place where previous search started = Display current line number / Search for kth occurrence of regular expression [1] n Search for kth occurrence of last r.e [1] ! or :! Execute in a subshell v Start up /usr/bin/vi at current line ctrl-L Redraw screen :n Go to kth next file [1] :p Go to kth previous file [1] :f Display current file name and line number . Repeat previous command
Personally, I prefer using less command over more command.
I hope this tutorial gave you enough to survive the more command. I welcome your questions and suggestions.