How to Use the Linux Command more and 5 Essential Options

The Linux Command more is commonly used in situations where you need to read text files on Linux. Instead of loading the entire file or output all at once, more allows you to view content in portions that fit within the display area. In this post, we’ll introduce the basic usage of the more command, various options, and some useful tips.

What is the Linux Command more?

The Linux Command more is used to display the content of a text file or screen output in pages. It doesn’t display the entire content at once but allows the user to view it one screen at a time. The more command is particularly useful when reading long files and is frequently used during terminal work.

Basic Usage

The basic usage of the more command is straightforward. You simply type more followed by the file name.

more filename
ShellScript

For example, if you want to view a file named example.txt, you would enter the following:

more example.txt
ShellScript

When you execute this command, the content of example.txt is displayed one screen at a time. At the bottom of the screen, you’ll see something like “–more–(16%)” indicating how much of the file has been displayed. Pressing the spacebar will move to the next page. In addition to the spacebar, there are various key inputs that allow you to control the more command.

Figure 1. Linux Command more: Execution Result
Figure 1. Linux Command more: Execution Result

Major Key Operations

Here are some key inputs that can be useful when using the more command:

  • Spacebar: Move to the next page.
  • Enter key: Move down one line.
  • b key: Go back to the previous page.
  • q key: Exit the more command and stop viewing the file.
  • = (equals) key: Display the current cursor position as a line number.
  • / (slash) key: Search for a specific string using regular expressions. For example, typing /search will search for the word “search”.
  • v key: Edit the current view using the /usr/bin/vi editor.
  • h key: Display instructions on how to use the more command.

Familiarizing yourself with these basic key operations will greatly aid in navigating files.

Key Options for the more Command

The more command offers various options beyond its basic functionality. By utilizing these options, you can navigate files more efficiently.

-d Option: Display Help

Using the -d option allows the display of help messages without triggering an alert sound.

more -d example.txt
ShellScript

Since it’s not a file output, the v key cannot be used, so pressing v will trigger a message like “[Press ‘h’ for instructions.]”.

Figure 2. Linux Command more: Displaying Help with the -d Option
Figure 2. Linux Command more: Displaying Help with the -d Option

-c Option: Print Over

The -c option clears only the remaining parts of the lines already displayed after printing text. This option helps keep the screen clean when reading a file.

more -c example.txt
ShellScript

-p Option: Clean Print

The -p option works differently from the -c option. It clears the entire screen before displaying text.

more -p example.txt
ShellScript

+num Option: Start from a Specific Line

This option is useful when you don’t want to start reading a file from the beginning but from a specific line. For example, to start viewing from the 10th line, you would enter the following:

more +10 example.txt
ShellScript

In this example, I entered the command below to view the results of ps ax from the 10th line:

ps ax | more +10
ShellScript

As expected, the output starts from the 10th line.

Figure 3. Linux Command more: Displaying from a Specific Line with +n Option
Figure 3. Linux Command more: Displaying from a Specific Line with +n Option

-s Option: Squeeze Blank Lines

The -s option compresses consecutive blank lines into a single blank line. For example, if there are three blank lines, this option displays them as one. This option works similarly to the -s option in the cat command, reducing unnecessary blank lines to make the screen usage more efficient.

more -s example.txt
ShellScript

Useful Tips

Displaying Only Part of the File Content

The more command becomes even more powerful when combined with other commands. For example, by using it with the head or tail commands, you can view only the beginning or end of a file with more.

tail -n 100 /var/log/syslog | more
ShellScript

The above command outputs the last 100 lines of the syslog file, one page at a time using the more command. Similarly, using the head command, you can view the beginning part of a file.

Figure 4. Linux Command more: Using with a Pipeline and the tail Command
Figure 4. Linux Command more: Using with a Pipeline and the tail Command

Setting Environment Variables

If you want to change the default behavior of the more command, you can set environment variables. For example, if you want to use skipping blank lines (-s) and clearing the screen (-c) as default options, you can configure it in your .bashrc or .zshrc file as follows:

export MORE='-c -s'
~/.bashrc or ~/.zshrc

With this setting, you won’t need to add options each time you enter a command, making it more convenient.

Precautions

The more command is suitable for simply reading files, but it may not perform well with very large files. Additionally, it’s not suitable for editing or modifying file structures. For such tasks, it’s better to use editors like vim or nano.

Summary

The Linux Command more is a highly useful tool for effectively navigating text files. By mastering its basic usage and various options, you can make browsing long files much easier. Additionally, combining it with other commands or setting environment variables can lead to even more efficient workflows. However, for very large files or when editing is necessary, it’s advisable to use other tools. By effectively utilizing the Linux command more, you can significantly enhance your productivity in a Linux environment.

References

Leave a Comment