Viewing Text Files
Linux provides several commands to read or preview the contents of text files directly from the terminal.
cat
– Concatenate and Display File
Displays the entire contents of a file.
Syntax
cat filename
Usage
cat /etc/passwd
Flags
-n
: Number all output lines-b
: Number non-blank lines
Notes
- Not ideal for large files (scrolls entire content rapidly).
head
– View Beginning of File
Shows the first 10 lines by default.
Syntax
head filename
Usage
head -n 15 error.log # First 15 lines
Flags
-n <number>
: Number of lines to display
tail
– View End of File
Shows the last 10 lines by default.
Syntax
tail filename
Usage
tail -n 20 access.log # Last 20 lines
Flags
-n <number>
: Number of lines-f
: Follow the file (live update as new lines are added, useful for logs)
Example
tail -f /var/log/syslog
less
– Scrollable Viewer
Allows scrolling through a file page by page and line by line.
Syntax
less filename
Usage
less /var/log/dmesg
Navigation inside less
SPACE
: Next pageb
: Back one pageq
: Quit/pattern
: Searchn
: Next search result
Summary Table
Command | Default Output | Good For | Live Updates | Searchable | Scrollable |
---|---|---|---|---|---|
cat | Full file | Small files | No | No | No |
head | First 10 lines | Quick preview | No | No | No |
tail | Last 10 lines | Logs, end of file | Yes (-f ) | No | No |
less | Page-wise view | Large, log files | No | Yes | Yes |
Last updated on