Linux View File
In Linux, there are several commands to view the contents of text files, each with its own strengths and use cases. Whether you need to quickly glance at a small file, paginate through a large log, or monitor a file in real-time, the command line offers efficient tools.
cat Command
The cat (concatenate) command is one of the simplest ways to display the entire content of a file to standard output. It's best suited for small to medium-sized files, as it will dump the entire content to your terminal at once.
Syntax
Examples
View a simple text file
Displays the content of the os-release file.
View multiple files
Concatenates and displays the content of both files.
less Command
The less command is a powerful pager that allows you to view file contents one screen at a time. It's ideal for large files because it doesn't load the entire file into memory, and it allows both forward and backward navigation.
Syntax
Examples
View a large log file
Opens the syslog file in a pager, allowing you to scroll through it.
Search within a file (inside less)
Searches for a specific term within the opened file.
more Command
Similar to less, the more command also displays file contents page by page. However, more is less flexible than less as it primarily allows forward navigation only.
Syntax
Examples
View a file page by page
Displays the content of long_document.txt one screen at a time.
head Command
The head command displays the beginning (first 10 lines by default) of a file. It's useful for quickly checking the start of a log file or a configuration file.
Syntax
Examples
View the first 10 lines
Displays the first 10 lines of access.log.
View the first 20 lines
Displays the first 20 lines of configuration.conf.
tail Command
The tail command displays the end (last 10 lines by default) of a file. It's commonly used for monitoring log files in real-time, as it can continuously output new lines as they are added.
Syntax
Examples
View the last 10 lines
Displays the last 10 lines of the authentication log.
Monitor a log file in real-time
Continuously displays new lines appended to the Nginx error log.
View the last 50 lines
Displays the last 50 lines of system.log.