How to View File Content in Linux: Simple Commands Explained
To view file content in Linux, use the
cat command to display the whole file, or less to scroll through it page by page. For quick previews, head and tail show the start or end of a file respectively.Syntax
Here are common commands to view file content in Linux:
cat filename: Shows the entire file content at once.less filename: Opens the file for scrolling up and down.head filename: Displays the first 10 lines by default.tail filename: Displays the last 10 lines by default.
Replace filename with your actual file name or path.
bash
cat filename less filename head filename tail filename
Example
This example shows how to use cat and head to view a file named example.txt.
bash
echo -e "Line 1\nLine 2\nLine 3\nLine 4\nLine 5" > example.txt cat example.txt head -n 3 example.txt
Output
Line 1
Line 2
Line 3
Line 4
Line 5
Line 1
Line 2
Line 3
Common Pitfalls
Common mistakes when viewing files include:
- Trying to
catvery large files, which floods the terminal. - Not using
lessfor long files, missing the ability to scroll. - Forgetting to specify the correct file path, causing errors.
Use less for large files and check file paths carefully.
bash
cat largefile.txt # May flood terminal
less largefile.txt # Better for large filesQuick Reference
| Command | Description |
|---|---|
| cat filename | Show entire file content |
| less filename | View file with scrolling |
| head filename | Show first 10 lines |
| head -n N filename | Show first N lines |
| tail filename | Show last 10 lines |
| tail -n N filename | Show last N lines |
Key Takeaways
Use
cat for small files to see all content at once.Use
less to scroll through large files comfortably.Use
head and tail to preview start or end of files.Always check the file path to avoid errors.
Avoid using
cat on very large files to prevent terminal flooding.