- 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
- Mastering File Reading in Linux: A Comprehensive Guide
- “cat” Command
- “nl” Command
- How to Display Contents Of A Text File On Command Line Linux
- Reading a File Line by Line with “read” Command
- “-r” Option with “read” Command
- “read()” Function
- Other Ways to Read Files
- Other code examples for reading files in Linux are also available
- Conclusion
- Frequently Asked Questions - FAQs
- What is the "cat" command in Linux, and how does it work for reading files?
- How can I read a file line by line in Bash?
- What is the "-r" option with the "read" command in Linux, and when should I use it?
- How does the "read()" function in Linux work for reading files?
- What are some other ways to read files in Linux besides the "cat" and "read" commands?
- Why is mastering file reading in Linux important?
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.
Mastering File Reading in Linux: A Comprehensive Guide
Learn how to read files in Linux with popular commands such as «cat» and «read». This guide covers different ways to read files, including reading a file line by line in Bash. Master file reading in Linux today!
- “cat” Command
- “nl” Command
- How to Display Contents Of A Text File On Command Line Linux
- Reading a File Line by Line with “read” Command
- “-r” Option with “read” Command
- “read()” Function
- Other Ways to Read Files
- Other code examples for reading files in Linux are also available
- Conclusion
- How do I read a file in Unix?
- How do I read the contents of a file in bash?
- How do you read each line in a file in Linux?
Linux is an operating system that is popular amongst developers, system administrators, and power users. One of the essential tasks when working with Linux is reading files. There are several ways to read files in Linux, including the popular “cat” command and the “read” command in Bash. In this post, we will cover different ways to read files and how to read a file line by line in Bash. By the end of this post, the reader will have a solid understanding of how to read files in Linux.
“cat” Command
The “cat” command is the simplest and most popular way to view a file in Linux. By default, “cat” displays the entire file content in the terminal. This command can also be used to concatenate multiple files and redirect output to a file. Here is an example of the “cat” command in action:
This command will display the contents of the “file.txt” file in the terminal.
“nl” Command
The “nl” command is similar to “cat” but adds line numbers to the output. This command is useful when working with large files or when it’s necessary to reference specific line numbers. Here is an example of the “nl” command in action:
This command will display the contents of the “file.txt” file with line numbers in the terminal.
How to Display Contents Of A Text File On Command Line Linux
How to Display Contents of a File in Linux. Linux Basics: How to Show File Contents#Ubuntu Duration: 9:48
Reading a File Line by Line with “read” Command
To read a file line by line in Bash, use the “read” command in a loop. This method is considered a best practice for reading files in Bash. Here is an example of how to use the “read” command to read a file line by line:
#!/bin/bashwhile read line; do echo $line done < file.txt
In this example, the “while” loop reads each line of the “file.txt” file and prints it in the terminal. The “done < file.txt” statement tells the loop to read from the “file.txt” file.
“-r” Option with “read” Command
The “-r” option with the “read” command is used to omit backslash escapes. This option is useful when reading files that contain special characters. Here is an example of how to use the “-r” option with the “read” command:
#!/bin/bashwhile read -r line; do echo $line done < file.txt
In this example, the “-r” option tells the “read” command to omit backslash escapes when reading each line of the “file.txt” file.
“read()” Function
The “read()” is a Linux function that attempts to read up to a certain number of bytes from a file descriptor. This function is useful for reading binary files or files with specific formats. Here is an example of how to use the “read()” function:
In this example, the “exec” command opens the “file.txt” file and assigns it to file descriptor 3. The “while” loop reads up to 4 bytes of data from file descriptor 3 and prints it in the terminal.
Other Ways to Read Files
The Linux “source” command reads and executes a file in the current shell. Here is an example of how to use the “source” command:
In this example, the “source” command executes the “script.sh” file in the current shell.
The “less” command can be used to view the contents of a file from the Linux command line. Here is an example of how to use the “less” command:
In this example, the “less” command opens the “file.txt” file in a pager for easy viewing and navigation.
Other code examples for reading files in Linux are also available
In Shell case in point, read file using shell script
#!/bin/bash input="/path/to/txt/file" while IFS= read -r line do echo "$line" done < "$input"
In Shell , in particular, how to open file in linux
Conclusion
Reading files is an essential task for Linux users, and there are several ways to accomplish this task. The “cat” command is the simplest and most popular, while the “read” command in Bash is considered a best practice. The “-r” option with the “read” command is useful when reading files with special characters, and the “read()” function is useful for reading binary files. Other commands such as “source” and “less” can also be used to read files. By mastering these techniques, the reader will be able to efficiently read files in Linux.
Frequently Asked Questions - FAQs
What is the "cat" command in Linux, and how does it work for reading files?
The "cat" command is a popular way to view the contents of a file in Linux. By default, it displays the entire file content in the terminal. It can also be used to concatenate multiple files and redirect output to a file.
How can I read a file line by line in Bash?
To read a file line by line in Bash, use the "read" command in a loop. This method is considered a best practice for reading files in Bash.
What is the "-r" option with the "read" command in Linux, and when should I use it?
The "-r" option with the "read" command is used to omit backslash escapes. This option is useful when reading files that contain special characters.
How does the "read()" function in Linux work for reading files?
The "read()" function attempts to read up to a certain number of bytes from a file descriptor. This function is useful for reading binary files or files with specific formats.
What are some other ways to read files in Linux besides the "cat" and "read" commands?
The Linux "source" command reads and executes a file in the current shell. The "less" command can be used to view the contents of a file from the Linux command line.
Why is mastering file reading in Linux important?
Reading files is an essential task for Linux users, and mastering different techniques to read files efficiently can save time and improve productivity.