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 filenameUsage
cat /etc/passwdFlags
-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 filenameUsage
head -n 15 error.log # First 15 linesFlags
-n <number>: Number of lines to display
tail – View End of File
Shows the last 10 lines by default.
Syntax
tail filenameUsage
tail -n 20 access.log # Last 20 linesFlags
-n <number>: Number of lines-f: Follow the file (live update as new lines are added, useful for logs)
Example
tail -f /var/log/syslogless – Scrollable Viewer
Allows scrolling through a file page by page and line by line.
Syntax
less filenameUsage
less /var/log/dmesgNavigation 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