Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Viewing Files in Linux Using cat, more, and less
In Linux, we often need to view file contents without opening them in text editors like vi or vim. Three essential commands for file viewing are cat, more, and less. Each command serves different purposes depending on file size and viewing requirements. The cat command displays entire file content at once, while more and less provide paginated viewing for larger files.
The cat Command
The cat (concatenate) command is the most straightforward way to display file contents. It reads and displays the entire file content on the terminal screen.
Basic File Viewing
To display the complete content of a file:
$ cat filename.txt
This is line 1 This is line 2 This is line 3 File content ends here
Displaying Line Numbers
Use the -n option to show line numbers:
$ cat -n filename.txt
1 This is line 1
2 This is line 2
3 This is line 3
4 File content ends here
The more Command
The more command displays file content one screen at a time, making it suitable for viewing large files. It loads the entire file into memory before displaying.
Viewing Large Files
Create a sample file with command history:
$ history > large_file.txt $ more large_file.txt
1 ls -la
2 pwd
3 cd Documents
4 mkdir projects
5 cat /etc/passwd
6 grep root /etc/passwd
7 find / -name "*.log" 2>/dev/null
8 ps aux | grep python
9 top
10 htop
11 df -h
12 du -sh *
13 wget http://example.com/file.zip
14 curl -O http://example.com/data.json
15 tar -xzf archive.tar.gz
--More--(15%)
Navigation controls in more:
Enter Move one line forward
Spacebar Move one page forward
q Quit the viewer
Viewing Multiple Files
$ more file1.txt file2.txt
:::::::::::::: file1.txt :::::::::::::: Content of first file --More--(Next file: file2.txt)
The less Command
The less command is an enhanced version of more with additional features and better performance. It doesn't load the entire file into memory, making it faster for large files.
Basic Usage
$ less large_file.txt
Navigation controls in less:
Arrow keys/j/k Move line by line
Spacebar/Page Down Move one page forward
Page Up/b Move one page backward
/pattern Search forward
?pattern Search backward
q Quit
Displaying Line Numbers
$ less -N large_file.txt
Comparison
| Feature | cat | more | less |
|---|---|---|---|
| File Size Handling | All sizes (dumps everything) | Large files (paginated) | Large files (paginated) |
| Memory Usage | Low | High (loads entire file) | Low (loads as needed) |
| Navigation | None | Forward only | Forward and backward |
| Search Function | No | Limited | Full search capability |
| Speed | Fast (instant display) | Slower (loads all) | Fastest (on-demand loading) |
Key Points
cat is ideal for small files and quick content display
more provides basic pagination but loads entire files into memory
less offers superior performance and navigation features for large files
Use
lesswhen you need to search, navigate, or view very large files efficiently
Conclusion
Choose cat for quick viewing of small files, more for basic pagination needs, and less for advanced file viewing with large files. The less command is generally preferred for large files due to its efficient memory usage and superior navigation capabilities.
