Linux provides multiple commands to view file contents.
Most commonly used commands:
- cat
- less
- more
- head
- tail
1. cat Command
Meaning
cat = concatenate
Used to:
- View file contents
- Combine files
- 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:
- Logs
- Large configs
- Large text files
Syntax
less filename
Example
less /var/log/syslog
Features of less
- Scroll up/down
- Search text
- Efficient for large files
- 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
- Forward navigation
- 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:
- Older Unix systems
- 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)