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

cat [OPTION]... [FILE]...

Examples

View a simple text file

cat /etc/os-release

Displays the content of the os-release file.

View multiple files

cat file1.txt file2.txt

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

less [OPTION]... [FILE]...

Examples

View a large log file

less /var/log/syslog

Opens the syslog file in a pager, allowing you to scroll through it.

Search within a file (inside less)

# Inside less, type /search_term and press Enter # Press n for next match, N for previous match

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

more [OPTION]... [FILE]...

Examples

View a file page by page

more long_document.txt

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

head [OPTION]... [FILE]...

Examples

View the first 10 lines

head access.log

Displays the first 10 lines of access.log.

View the first 20 lines

head -n 20 configuration.conf

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

tail [OPTION]... [FILE]...

Examples

View the last 10 lines

tail /var/log/auth.log

Displays the last 10 lines of the authentication log.

Monitor a log file in real-time

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

Continuously displays new lines appended to the Nginx error log.

View the last 50 lines

tail -n 50 system.log

Displays the last 50 lines of system.log.

See also