Reading file content on a Linux terminal is a common task, especially for developers and system administrators. Whether you’re inspecting logs, checking configuration files, or viewing text documents, Linux offers several efficient commands for this purpose. In this guide, we’ll explore the most popular methods, along with their features and use cases.
1. cat
Command
The cat
command (short for "concatenate") is one of the simplest ways to display the entire contents of a file in the terminal.
cat filename.txt
This command outputs the content of filename.txt
directly to the terminal. It’s perfect for small files but less convenient for large ones.
2. less
Command
For longer files, the less
command is a better choice. It allows you to scroll through the content easily.
less filename.txt
- Use the arrow keys to navigate.
- Press
q
to exit.
The less
command is ideal for logs or lengthy documents.
3. more
Command
Similar to less
, the more
command displays content one screen at a time.
more filename.txt
- Press the space bar to move to the next page.
- Exit by pressing
q
.
While it’s simpler than less
, it lacks some of its navigation features.
4. head
Command
Need to preview only the first few lines of a file? Use the head
command:
head filename.txt
By default, it shows the first 10 lines. You can adjust this with the -n
option:
head -n 20 filename.txt # Show the first 20 lines
5. tail
Command
To check the last lines of a file, especially useful for log files, use tail
:
tail filename.txt
Customize the number of lines displayed with the -n
option:
tail -n 20 filename.txt # Show the last 20 lines
6. Using nano
or vim
for Interactive Viewing
If you want to view and edit the file, text editors like nano
and vim
are great choices.
nano filename.txt
Or:
vim filename.txt
These tools allow you to browse and modify the file’s content within the terminal.
Choosing the Right Command
- For quick viewing: Use
cat
. - For large files: Choose
less
ormore
. - For partial previews: Opt for
head
ortail
. - For editing: Open the file in
nano
orvim
.
Mastering these commands will make navigating and managing files on Linux more efficient and seamless. Experiment with each to find the best fit for your workflow!