Reading files in Linux offers many approaches, from simple viewing commands to powerful text processing utilities. Understanding how to read file in linux is a fundamental skill that every user, whether a beginner or a seasoned sysadmin, needs to master. Linux provides a rich set of command-line tools that let you peek into files, scroll through logs, or extract specific data without opening a heavy editor. In this guide, we’ll walk through the most common and effective methods, from basic commands to advanced tricks, so you can handle any text file with confidence.
Let’s start with the simplest tools. If you just want to quickly see what’s inside a file, the cat command is your go-to. It prints the entire file content to your terminal. But be careful with large files—it can flood your screen. For shorter files, it’s perfect.
Another handy command is less. It lets you scroll through a file page by page, which is ideal for logs or long documents. Press the spacebar to go forward, b to go back, and q to quit. It’s like a built-in reader.
For a quick look at the beginning of a file, use head. By default, it shows the first 10 lines. You can change that with the -n option, like head -n 20 filename. Similarly, tail shows the last 10 lines, which is great for checking recent log entries.
How To Read File In Linux
Now that you have a taste of the basics, let’s dive deeper into the core methods for reading files. The exact phrase “How To Read File In Linux” encapsulates the need to choose the right tool for the job. Whether you’re debugging a script, analyzing data, or just browsing, each command has its strengths.
Using Cat For Quick Viewing
The cat command, short for concatenate, is the simplest way to display file content. It reads the file and outputs it to the terminal. Here’s how to use it:
- Type
cat filename.txtand press Enter. - For multiple files, use
cat file1.txt file2.txtto show them one after another. - To add line numbers, use
cat -n filename.txt.
But cat has a downside: it dumps everything at once. For files longer than your terminal window, you’ll only see the last few lines. That’s where less shines.
Scrolling With Less And More
The less command is a pager—it lets you navigate through a file interactively. It’s more powerful than the older more command. To use it:
- Run
less filename.log. - Use arrow keys or
jandkto scroll line by line. - Press
Spaceto go forward a page,bto go back. - Search for text by typing
/followed by your search term, then press Enter. Pressnfor next match,Nfor previous. - Exit with
q.
One cool feature: less doesn’t load the entire file into memory, so it works well with huge files. The more command is similar but more limited—it only allows forward scrolling. Try more filename if you prefer a simpler tool.
Viewing File Beginnings And Endings
Sometimes you only need a snippet. The head and tail commands are perfect for that.
head -n 5 filenameshows the first 5 lines. Omit-nto get 10 lines by default.tail -n 5 filenameshows the last 5 lines. Usetail -f filenameto follow a file in real time—great for watching logs as they update.
For example, if you’re monitoring a server log, run tail -f /var/log/syslog and see new entries appear instantly. Press Ctrl+C to stop.
Reading Specific Lines Or Patterns
To extract specific content, combine commands with grep. This tool searches for patterns. For instance:
grep "error" filename.logshows all lines containing “error”.- Use
grep -ifor case-insensitive search. - Add
-nto show line numbers:grep -n "error" filename.log.
You can also use sed to print a specific line. For example, sed -n '10p' filename prints line 10. Or sed -n '5,10p' filename prints lines 5 through 10.
Using Awk For Column-Based Reading
If your file is structured like a table (e.g., CSV or space-separated), awk is your friend. It treats each line as a record and each word as a field. For example:
awk '{print $1}' filenameprints the first column.awk '{print $1, $3}' filenameprints columns 1 and 3.- You can also filter:
awk '/error/ {print $0}' filenameprints entire lines containing “error”.
Awk is powerful for data extraction and reporting. It’s worth learning the basics.
Reading Compressed Files
Linux files often come compressed to save space. You don’t need to decompress them first. Use these commands:
zcat file.gzworks likecatfor gzip files.zless file.gzis likelessfor compressed files.zgrep "pattern" file.gzsearches inside a compressed file.
For bzip2 files, use bzcat, bzless, and bzgrep. For xz files, use xzcat, xzless, and xzgrep.
Reading Binary Files
Not all files are text. For binary files, use hexdump or od. For example:
hexdump -C filenameshows a hex and ASCII representation.od -c filenamedisplays bytes as characters.
These are useful for debugging or inspecting file headers.
Using Editors For Reading
Sometimes you want to read a file with the ability to edit later. Command-line editors like nano, vim, or emacs let you view and modify. For read-only viewing, use view (which opens vim in read-only mode) or nano -v.
To open a file in nano without editing: nano -v filename. This prevents accidental changes.
Reading Multiple Files At Once
You can read several files sequentially using cat or less. For example:
cat file1.txt file2.txtshows both.less file1.txt file2.txtlets you navigate between them with:n(next) and:p(previous).
For a more visual approach, use vim with multiple files: vim file1.txt file2.txt. Switch with :next and :prev.
Reading Large Files Efficiently
When dealing with gigabyte-sized files, avoid loading them entirely. Use less or tail with -f. Another trick is to split the file with split and read chunks. For example:
split -l 1000 largefile.txt chunk_creates files of 1000 lines each.- Then read each chunk with
less chunk_aa.
You can also use head and tail together: tail -n +1000 largefile.txt | head -n 500 shows lines 1000 to 1499.
Reading Files With Line Numbers
To see line numbers while reading, combine cat -n with a pager. For instance:
cat -n filename | lessshows numbered lines in a scrollable view.- Or use
less -N filenameto display line numbers directly inless.
Reading Files From The Internet
You can read remote files without downloading them. Use curl or wget with a pipe. For example:
curl -s https://example.com/file.txt | lesswget -q -O - https://example.com/file.txt | head -n 20
This is handy for reading documentation or logs hosted online.
Reading Files With Syntax Highlighting
If you want color-coded output for code files, use pygmentize or highlight. Install them first, then:
pygmentize -g filename.py | less -Rhighlight -O ansi filename.py | less -R
The -R flag in less preserves color codes.
Common Pitfalls And Tips
Here are some mistakes to avoid:
- Don’t use
caton binary files—it will garble your terminal. Usehexdumpinstead. - Remember that
lessis more powerful thanmore. Stick withless. - If a file is huge, avoid
cator editors—usetailorless. - Always check file permissions with
ls -l; you need read permission.
Real-World Examples
Let’s put it all together with practical scenarios:
- Check system logs:
tail -f /var/log/syslogto monitor real-time. - Find errors in a log:
grep -i "error" /var/log/syslog | less. - Read a CSV file:
awk -F',' '{print $1, $2}' data.csvto extract columns. - View a compressed backup:
zless backup.log.gz.
Advanced: Reading Files With Scripts
You can automate reading with bash scripts. For example, a script to read a file line by line:
#!/bin/bash
while IFS= read -r line; do
echo "$line"
done < filename.txt
This is useful for processing each line.
Frequently Asked Questions
What Is The Easiest Way To Read A File In Linux?
The easiest way is using cat filename for short files or less filename for longer ones. Both are simple and require no options.
How Can I Read A File Without Opening An Editor?
Use command-line tools like cat, less, head, or tail. They display content directly in the terminal without launching an editor.
Can I Read A File That Is Being Written To?
Yes, use tail -f filename. It shows new lines as they are added, which is perfect for logs.
How Do I Read A Specific Part Of A File?
Use head for the beginning, tail for the end, or sed -n '5,10p' for a range of lines. grep helps find lines with specific text.
What If The File Is Compressed?
Use zcat, zless, or zgrep for gzip files. Similar commands exist for bzip2 and xz formats.
Now you have a comprehensive toolkit for reading files in Linux. Practice these commands on sample files to build muscle memory. Start with cat and less, then explore grep and awk as you get comfortable. Each tool has its place, and knowing which to use will save you time and frustration. Happy reading!