Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.

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
  • SPACE: Next page
  • b: Back one page
  • q: Quit
  • /pattern: Search
  • n: Next search result

Summary Table

CommandDefault OutputGood ForLive UpdatesSearchableScrollable
catFull fileSmall filesNoNoNo
headFirst 10 linesQuick previewNoNoNo
tailLast 10 linesLogs, end of fileYes (-f)NoNo
lessPage-wise viewLarge, log filesNoYesYes
Last updated on