Linux Command To View File

Linux Command To View File

Reading time1 min
#Linux#Command#File#Less#Tail#Head

Linux File Viewing: Efficient Techniques for Real-World Workloads

Parsing massive log files, reviewing configuration changes, or simply drilling into scripts for a quick audit—file viewing is fundamental to Linux operations. Yet, reliance on cat is common, usually until the volume or complexity of data punishes the scrollback buffer and productivity drops.

Let’s skip the ceremony and focus on practical, efficient strategies to interrogate files on Linux, with a granular look at the utility and quirks of less, head, tail, and lesser-known alternatives used by engineers in production environments.


When cat Fails: The Scalability Problem

A quick dump:

cat /etc/ssh/sshd_config

Useful for <100 lines, or when piping output directly. But cat falls apart in these scenarios:

  • Reading rotated logs (/var/log/syslog.1), >10k lines: terminal floods, scrollback truncates.
  • Inspecting files with unprintable characters: output mangles the terminal.
  • Looking for clues in hundreds of lines—searching, jumping, filtering? Not possible.

The takeaway: For inspection and navigation, cat just isn’t the answer.


less: Your Day-to-Day Workhorse

Less (usually v530+ on modern distros) provides fast, interactive paging with minimal memory overhead, even for GB-sized files. Typical workflow for log analysis:

sudo less /var/log/auth.log

Navigation keys:

  • Arrow/Page keys: Scroll line/page at a time.
  • /pattern + n/N: Incremental search forward/backward.
  • g/G: Jump to beginning/end; prepend number for line (e.g., 123G).
  • F: Enter follow mode—live tail with scrollback retention, unlike tail -f.
  • q: Quit safely regardless of file size.

Important nuance: Less reads files on-demand, not wholesale. Useful for remote NFS mounts or when memory is constrained.

Syntax highlighting (if supported in your environment):

less -R /etc/nginx/nginx.conf | source-highlight

or (with bat installed, see tip below):

bat /etc/nginx/nginx.conf

Case-insensitive search:

  • Permanent: export LESS='-i'
  • On demand (inside less): Press -I

Known caveat: Some multi-megabyte single-line log files (usually due to broken application logging) still degrade navigation speed.


tail: Real-Time Log Inspection

Standard tail—ubiquitous across RHEL 7/8/9, Ubuntu LTS, Alpine—is critical for streaming diagnostics.

tail -n 30 /var/log/kern.log

Practical application: Combining tail -f with grep or awk for targeted monitoring.

tail -F /var/log/syslog | grep --line-buffered 'sshd'
  • -F retries on log rotation (common with systemd or logrotate).
  • --line-buffered prevents output buffering delay.

Side note: For distributed logging, check timestamp drift directly from multiple boxes:

tail -f /srv/app/logs/*.log | ts

(ts from moreutils, adds timestamps.)


head: The Front Loader

Previewing the start of a CSV, config, or error trace:

head -20 /var/tmp/huge_import_20240610.csv

head is invaluable before ingesting, say, a 50GB gzipped dump:

zcat large.dump.gz | head -40

Tip: Always confirm field headers or version markers with head before batch scripting over files.


Beyond the Defaults: Hidden Gem Utilities

  • most: Niche but effective, especially under tmux or when viewing horizontally-wrapped output.

    • most /var/log/httpd/access.log enables horizontal scroll (critical for web log inspection with long User-Agents).
  • view: Launches vim in read-only mode for accidental edit prevention.

    • view /etc/fstab
    • vim -R behaves similarly.
  • bat (>=0.18.0): Drop-in cat/less hybrid with syntax highlighting, line numbers, and Git-awareness.

    • bat ~/.bashrc
  • Gotcha: Some less-alikes (e.g., more, default on legacy Solaris/AIX) do not support backward navigation or search—avoid unless forced.


Side-by-Side: Commands for File Inspection

CommandTypical UseNotable Flags/WorkflowComments
catSmall file dump, pipingAvoid for files >1000 lines
lessInteractive nav/search/, F, -i, -RHandles GB-scale logs gracefully
headPreview file start-n <num>Use before batch operations
tailMonitor/log follow-f, -F, -n, `grep`
mostAdvanced navigationBest for wide logs
viewVim UI (read-only)Great for accidental edit proof
batHighlight, side numbersImproved visibility, not POSIX

Pragmatic Patterns

  • For daily triage:

    less +F /var/log/mysql/error.log
    

    Observe issues live, scrollback with Ctrl-C.

  • For sudden spikes in web requests (e.g., DDoS diagnostics):

    tail -n 500 access.log | grep 'POST /api'
    
  • Searching for changed configuration within a versioned file:

    git diff nginx.conf | less
    
  • For disaster debugging (single-line logs from an application misconfigured with '\n' in payload):

    less -RS broken.log
    

Recap

Choosing the right tool directly affects operational throughput. For interactive inspection: default to less. For continuous monitoring: prefer tail -F. For automation or quick checks: head and cat (in controlled doses). Augment with community tools (bat, most) for specialty use cases.

One last pointer: Always be aware of your terminal’s $LINES variable—incorrect values from, for example, SSH under tmux, can break paging in less/most.


Try It Yourself

Now, compare less +F /var/log/syslog and tail -F /var/log/syslog—notice retained scrollback after live-following in less. Running both alongside a log-generating process (logger test) exposes subtle behavioral gaps.

Note: These approaches scale poorly on files with hundreds of megabytes of a single line—log rotation or preprocessing may be needed.


Have a pattern or edge case that isn’t covered here? Engineers often have local one-liners that solve these edge problems—worth comparing in your own environment.