DEV Community

Aryan Vaishnani
Aryan Vaishnani

Posted on

Viewing Files (cat, less, more, head, tail)

Linux provides multiple commands to view file contents.

Most commonly used commands:

  1. cat
  2. less
  3. more
  4. head
  5. tail

1. cat Command

Meaning

cat = concatenate

Used to:

  1. View file contents
  2. Combine files
  3. Create files quickly

Syntax

cat filename

Example

cat notes.txt

Output:

Hello Linux

Welcome to DevOps

View Multiple Files

cat file1.txt file2.txt

Create File Using cat

cat > test.txt

Type content and press:

CTRL + D

Important Options

Option Purpose
-n Show line numbers
-b Number non-empty lines
-s Remove repeated empty lines

Real-World Usage

View Configuration File

cat /etc/hosts

Check Kubernetes YAML

cat deployment.yaml

Limitation of cat

Not ideal for large files because:

  • Entire file prints at once.

2. less Command

Purpose

Used to read large files interactively.

Best command for:

  1. Logs
  2. Large configs
  3. Large text files

Syntax

less filename

Example

less /var/log/syslog

Features of less

  1. Scroll up/down
  2. Search text
  3. Efficient for large files
  4. Does not load full file into memory immediately

Navigation Keys

Key Action
Arrow Keys Scroll
Space Next page
b Previous page
/word Search
n Next search
q Quit

Real-World Usage

Monitor Logs

less /var/log/nginx/access.log

Read Large Config

less kubeconfig.yaml

Why less is Preferred

Common saying:

less is more

Because:

  • Better than more
  • More flexible navigation

3. more Command

Purpose

Views file content page by page.

Older version of file viewer.

Syntax

more filename

Example

more largefile.txt

Features

  1. Forward navigation
  2. Page-by-page reading

Limitation

Cannot scroll backward easily.

Because of this, less is preferred in modern Linux systems.

Real-World Usage

Still found in:

  1. Older Unix systems
  2. Minimal environments

4. head Command

Purpose

Displays first lines of a file.

Default:

First 10 lines

Syntax

head filename

Example

head app.log

Show Specific Number of Lines

head -5 app.log

Shows:

First 5 lines

Real-World Usage

Check Log Start

head -20 /var/log/auth.log

Verify CSV Header

head users.csv

5. tail Command

Purpose

Displays last lines of a file.

Default:

Last 10 lines

Syntax

tail filename

Example

tail app.log

Show Specific Number of Lines

tail -20 app.log

Real-Time Monitoring

Most important feature:

tail -f app.log

  • f = follow live updates

Real-World Usage

Monitor Application Logs

tail -f /var/log/nginx/error.log

Kubernetes Logs

kubectl logs -f pod-name

Internally similar to tail -f.

Comparison Table

Command Best For
cat Small files
less Large files
more Basic paging
head First lines
tail Last lines/log monitoring

Top comments (0)