Home Learn Linux 6 Linux Commands to View File Content Like a Pro

6 Linux Commands to View File Content Like a Pro

From the simple yet versatile cat command to the more advanced features of less and tail, we'll cover everything you need to know to efficiently explore file content in Linux.

by Divya Kiran Kumar
6 linux commands file content viewing

Linux is a powerful and versatile operating system that provides users with a robust command-line interface for managing files, processes, and systems. One of the most common tasks Linux users perform is viewing a file’s content. This article will introduce you to six essential commands for viewing the content of a file in the Linux command line: cat, tac, less, more, head, and tail. Each command has its unique features, advantages, and limitations, which we will discuss in detail below.

Before we head out to give you a step-by-step guide, let’s understand why you may want to view the contents of a file in the terminal in the first place.

Reasons to view the contents of a file in a Linux terminal

There are several reasons why you might want to view the contents of a file in Linux using the terminal:

  • Debugging: If you’re a programmer, you might want to view the contents of a log file to debug your application.
  • Troubleshooting: If you’re experiencing issues with a particular file or system process, viewing the contents of relevant files can help you diagnose the problem.
  • Configuration: Many Linux applications store their configuration settings in files. By viewing the contents of these files, you can modify the settings to customize the application to your needs.
  • Viewing System Information: Linux system administrators often need to view system information to monitor system health or troubleshoot issues. The contents of system logs, error logs, and other system files can provide valuable information in these situations.
  • Analyzing Data: If you have a file with data that you need to analyze, viewing the file’s contents in the terminal can be a quick way to get an overview of the data.

In general, viewing the contents of a file in Linux using the terminal is a quick and efficient way to get information about a file or system. It also provides a level of flexibility and control that GUI-based file viewers may not offer.

Viewing the content of a file in a Linux terminal

1. cat

The cat (short for “concatenate”) command is one of the most widely used commands for displaying the content of a file in the Linux command line. It reads the data from one or more input files and writes it to the standard output (usually the terminal). The syntax for using cat is:

cat [options] [file(s)]

Example:

cat file.txt

This command will display the entire content of file.txt.

using cat command

Using cat command

You can also view the contents of multiple files by specifying their names separated by spaces:

cat file1 file2 file3

By default, cat displays the file’s contents in one long stream. However, you can use the -n option to display the contents of the file with line numbers:

cat -n filename
cat command usage with line numbers

Cat command usage with line numbers

You can also use the > operator to redirect the output of cat to a new file. For example:

cat file1 > newfile

This will create a new file named newfile with the contents of file1.

2. tac

Similar to cat, tac is a command-line tool in Linux that is used to view the contents of a file. However, while cat displays the contents of a file in forward order, tac displays the contents in reverse order, meaning the last line of the file is displayed first, and so on.

The basic syntax for using tac is similar to cat:

tac [options] [file(s)]

To view the contents of a file in reverse order, enter the tac command followed by the name of the file:

tac filename

This will display the file’s contents in reverse order directly in the terminal.

However, unlike the cat command, note that the tac command doesn’t have a -n option to display the line numbers. If you need to display line numbers while using tac, you can use the nl command, which can be piped together with tac. For example:

tac filename | nl
tac command usage with with line numbers

Tac command usage with with line numbers

This will display the contents of the file in reverse order, along with line numbers. The nl command adds line numbers to the output, so by piping the output of tac to nl, you can get the line numbers for the reversed lines.

Alternatively, you can use the -r option with nl to display line numbers in reverse order. For example:

tac filename | nl -r

This will display the contents of the file in reverse order, along with the line numbers also in reverse order.

You can also use the > operator to redirect the output of tac to a new file:

tac file1 > newfile

This will create a new file named newfile with the contents of file1 in reverse order.

Overall, the tac command is a valuable tool for quickly viewing the contents of a file in reverse order, which can be helpful in certain situations, such as when analyzing log files.

3. less

the less command is another commonly used command-line tool in Linux that can be used to view the contents of a file. less is similar to cat but provides more functionality for scrolling through and searching the file’s contents.

To view the contents of a file using less, enter the command followed by the name of the file:

less filename
less command usage

less command usage

This will open the file in the less viewer. Once the file is open in less, you can scroll through the contents of the file using the arrow keys or the Page Up and Page Down keys. To quit less, press the q key.

In addition to scrolling through the file, less provides several other features, such as the ability to search for specific words or phrases in the file. To search for a word or phrase, press the / key followed by the word or phrase you want to search for. less will highlight all occurrences of the word or phrase in the file.

You can also use the -n option with less to display line numbers in the output:

less -N filename
less command usage with line numbers

less command usage with line numbers

This will display the contents of the file with line numbers in the left margin.

less also has a reverse option -r, which is used to view files in reverse order. This can be useful when you want to view the contents of a file in reverse order, similar to using tac.

Here is the basic syntax for using less with the -r option:

less -r [filename]

To view a file in reverse order using less, enter the command followed by the name of the file:

less -r filename

This will open the file in the less viewer in reverse order. Once the file is open in less, you can scroll through the contents of the file in reverse order using the arrow keys or the Page Up and Page Down keys.

In addition to scrolling through the file in reverse order, less provides all the same features as when viewing the file in forward order, such as the ability to search for specific words or phrases in the file using the / command.

The less command is a powerful tool for viewing the contents of a file in the Linux terminal and provides more functionality than the simple cat or tac commands.

4. more

The more command is another command-line tool in Linux that can be used to view the contents of a file, similar to less. However, more is an older and less commonly used utility than less.

To view the contents of a file using more, enter the command followed by the name of the file:

more filename

This will open the file in the more viewer. Once the file is open in more, you can scroll through the contents of the file using the arrow keys or the space bar. To quit more, press the q key.

more is similar to less but provides fewer features for scrolling and searching through the file. For example, you can only scroll forward through the file with more, and searching for specific words or phrases is not as straightforward as with less.

However, more does provide a few valuable options for viewing the contents of a file. For example, you can use the -num option to specify the number of lines to display on each screen:

more -10 filename
more command usage

more command usage

This will display the file’s contents with 10 lines displayed on each screen. You can use the up and down arrow keys to scroll to the next or previous set, which is extremely useful when dealing with a file with lots of data.

While more is a less commonly used utility than less, it can still be a valuable tool for quickly viewing the contents of a file in the Linux terminal in a controlled scroll. However, less is typically a better choice if you need more advanced features for scrolling and searching through the file.

5. head

The head command is another commonly used command-line tool in Linux that can be used to view the contents of a file. head is used to display the first few lines of a file and is useful when you only need to view the beginning of a large file.

The basic syntax for using head is:

head [options] [filename]

To view the first 10 lines of a file, enter the command followed by the name of the file:

head filename
head command usage

Head command usage

This will display the first 10 lines of the file directly in the terminal. By default, head displays the first 10 lines of the file, but you can also specify the number of lines to display using the -n option:

head -n 20 filename

This will display the first 20 lines of the file.

You can also use the > operator to redirect the output of head to a new file:

head file1 > newfile

This will create a new file named newfile with the first 10 lines of file1.

Overall, the head command is a simple and useful tool for quickly viewing the beginning of a file in the Linux terminal.

6. tail

tail is used to display the last few lines of a file and is useful when viewing the most recent entries in a log file or monitoring the progress of a file being written to.

The basic syntax for using tail is:

tail [options] [filename]

To view the last 10 lines of a file, simply enter the command followed by the name of the file:

tail filename
tail command usage

tail command usage

This will display the last 10 lines of the file directly in the terminal. By default, tail displays the last 10 lines of the file, but you can also specify the number of lines to display using the -n option:

tail -n 20 filename

This will display the last 20 lines of the file.

You can also use the -f option to monitor the file for changes and display any new lines that are added to the end of the file in real-time:

tail -f filename

This can be useful for monitoring log files or other files being written to in real time.

You can also use the > operator to redirect the output of tail to a new file:

tail file1 > newfile

This will create a new file named newfile with the last 10 lines of file1.

Overall, the tail command is a useful tool for quickly viewing the last few lines of a file in the Linux terminal and monitoring changes to a file in real time.

Conclusion

In this article, we have discussed six essential commands for viewing the content of a file in the Linux command line: `cat`, `tac`, `less`, `more`, `head`, and `tail`. Each command has its unique features and use cases, so it’s essential to familiarize yourself with them to work efficiently with files in Linux. Consider the manual pages (`man <command>`) for detailed information on each command’s options and usage.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.