How to Open a Text File in Linux

Knowing how to open, read and quickly navigate text files is necessary in Linux.  If you want to view text files using the Linux GUI, you can simply open them with a text editor. However, Linux also offers a number of options to open text files from the command line.

The commands featured below are important for Linux users to know. Not only will they allow you to view everyday text files, but also look at your Linux configuration, log, and script files. As a Linux user, having the ability to open and view these files is essential in troubleshooting and maintaining the health of your system.

Note: For this guide, we’re using Zorin OS 16, an Ubuntu-based distro. The Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro.

Using the cat Command

The cat command, short for concatenate, is the most basic Linux command to view or open files in the Linux command line.

In our example, we’re viewing the /var/log/syslog file. The system log (/var/logs/syslog) contains a record of significant events related to the Linux operating system.

In the example below, we view the /var/log/syslog using the cat command.

# cat /var/log/syslog
Viewing /var/log/syslog with the cat command.
Viewing /var/log/syslog with the cat command.

Using the nl Command

The nl command displays files like the cat command but with line numbers.

# nl /var/log/syslog
Viewing /var/log/syslog with the nl command.
Viewing /var/log/syslog with the nl command.

Using the more Command

The more command is much like the cat command. However, unlike the cat command, the more command allows you to scroll through the text file. This is especially useful if the file is large, such as the /var/log/syslog file.

# more /var/log/syslog
Viewing /var/log/syslog with the more command.
Viewing /var/log/syslog with the more command.

Note the percentage displayed at the bottom of the window. Press <Enter> to scroll one line at a time. Use <Spacebar> to scroll one screen at a time. Scroll backward with <b>. Use </> to search for text within the document. To exit, press <q>. For help, press <h>.

Using the less Command

The less command is similar to the more command, but with more options. It’s also faster since it only loads one page at a time.

Similar to more, the less command allows you to view the contents of a file and navigate through the file. The main difference between more and less is that the less command is faster since it does not load the entire file at once but rather one screen at a time.

# less /var/log/syslog
Viewing /var/log/syslog with the less command.
Viewing /var/log/syslog with the less command.

Scrolling the file using the less command also differs. Press <Enter><h> for a list of these differences. To exit, press <q><Enter>.

Using the most Command

The most command is like the less and more command, but even more powerful. The more command pauses after each page and prints the file name, current line, and the percentage of the file displayed. What makes the most command more powerful is that it can display an arbitrary number of windows so long as each window occupies at least two screen lines. The windows can contain a different file or the same file. Additionally, the windows have their own mode. For example, one window may truncate the lines while the other window displays the file with its lines wrapped.

# most /var/log/syslog
Viewing /var/log/syslog with the most command.
Viewing /var/log/syslog with the most command.

The most command is also different in that you can use the arrow keys (↓ ↑ → ←) to scroll through the file. For help, press <H><Enter>, To exit, press <Q><Enter>.

Using the head Command

By default, the head command displays the first ten lines of the file. It also allows you to show a specified number of lines and the specified number of bytes.

# head /var/log/syslog
Viewing /var/log/syslog with the head command.
Viewing /var/log/syslog with the head command.

Using the tail Command

The tail command is the exact opposite of the head command. Instead of the first ten lines of a file, the tail command prints the last ten lines. But, the real power of the tail command is when used with the -f option (tail -f). This displays the file in real-time. This is especially useful for admins when troubleshooting system errors and developers when debugging a program.

# tail -f /var/log/syslog
Viewing /var/log/syslog with the tail -f command.
Viewing /var/log/syslog with the tail -f command.

To exit from tail -f, press <Ctrl><c>.

Other Types of Text Files

While all of the files we’ve featured here are with .txt and .log files, there are other types of text files to view with the commands shown above, such as Python (.pl), bash script (.sh), PHP (.php) files, as well as many others.

Commands used in this guide

  • cat – concatenate files and print on the screen.
  • nl – display and number the lines of a text file.
  • more – file perusal filter viewing a text file.
  • less – the opposite of more.
  • most – browse or page through a text file.
  • head – output the first part of a text file.
  • tail – output the last part of a text file.

Last words

Linux gives users numerous ways to open and view text files using the command line. We strongly encourage both new and veteran Linux users to test out the various commands above. Gaining an in-depth understanding of these commands will enrich your experience with Linux and aid greatly when troubleshooting issues with your system(s).

Leave a Comment