The more command in Linux is used to display file contents in the terminal. You can choose the structure of viewing and also search inside a file using the command. In this article, you will find the most useful examples of the more command in Linux.
A. Description
The more command in Linux works like a filter for viewing file contents in the terminal. It allows you to navigate through the file by creating an interactive environment. Following are some of the keyboard shortcuts that enable users to operate on the displayed contents.
- The ENTER Key: Scrolls down line by line.
- The B/ CTRL+B key: Goes backward a page of the content.
- The SPACE BAR key: Goes to the next page of the content.
- The forward slash (/) key: Searches for a string.
- The H key: Displays the help page content in the terminal. It provides a summary of the shortcut keys. The help page of the more command is as follows.
Note: Similar to the more command in Linux the cat command also displays file contents in the terminal. However, the cat command is not interactive and does not allow navigation or scrolling. Whereas the more command offers user interaction.
B. Syntax
The syntax of the more command in Linux simply contains single or multiple options and then the desired Filenames.
more [OPTION]... FILE…
Note: In the above syntax OPTION enclosed by square brackets and followed by 3 dots represents that OPTION is not mandatory and multiple OPTIONs can be utilized at the same time. Similarly, 3 dots after FILE suggest that you can specify multiple files as well.
C. Options
The more command in Linux offers several options to modify your experience of viewing file contents. However, you will find the most useful ones here for your convenience. For further features, you can always look at the man page.
man more
Useful Options
- -c/–clear-print: Prints each screen from the top clearing previous contents.
- -d/–silent: Display quit and help instructions on the terminal.
- -f/–no-pause: Does not fold the lines to the terminal.
- -n/–lines NUM: Specify the number of lines per screen.
- +number: Starts displaying from the specified line number.
- -p/–print-over: Cleans screen before displaying the text.
- -s/–squeeze: Suppresses multiple blank lines into one.
- +/string: Searches for the specified string inside a file.
NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.
Practical Examples of the “more” Command in Linux
The more command in Linux is a convenient command to read files from the terminal. Now, Some of the most useful applications of the more command have been illustrated below.
Example 1: Displaying File Contents in the Terminal Using the “more” Command in Linux
You can display file contents in the terminal using the more command in Linux. Using the available shortcut keys you will be able to navigate through the contents. In this example, I will view the file “largetext.txt” from my terminal. You can also do the same by following the given instructions.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more largetext.txt
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have displayed the “largetext.txt” file contents in my terminal.
Example 2: Displaying File Contents With Instructions Using the “more” Command in Linux
You can display file contents in the terminal with additional instructions using the more command in Linux. To do so you will need to use option -d. In this example, I will view the contents of the file “largetext.txt” with instructions in my terminal. You can follow the steps below to do the same.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more -d largetext.txt
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have displayed the “largetext.txt” file contents in my terminal with instructions.
Example 3: Displaying One Screen At a Time Using the “more” Command in Linux
You can extract the file contents one page at a time using the more command in Linux with the -c option. In this case, the page refers to the screen size of the terminal. Moving on to the next page will clear the previous contents displayed on the terminal and fill the screen with new lines. The same method applies when moving backward the file contents. In this example, I will view one screen of contents at a time of the file “largetext.txt”. You can also do the same by following the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more -c largetext.txt
➌ Now, press the ENTER button.
Output >
In the given image, you can see that I have printed the file contents on the whole terminal screen.
Example 4: Clearing Screen and Displaying File Contents Using the “more” Command in Linux
You can clear the screen before printing the file contents using the more command in Linux with the -p option. In this example, I will view the contents of the file “largetext.txt” in a clear terminal. To do the same follow the steps given below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more -p largetext.txt
➌ Tap the ENTER button.
Output >
In the below image, you can see that I have printed the file contents on a clear terminal screen.
Example 5: Avoid Folding Long Lines Using the “more” Command in Linux
You can stop wrapping long lines to the terminal using the more command in Linux with the -f option. In this example, I will stop folding lines to the screen while viewing the “largetext.txt” file.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Then, Type the following command in the command prompt:
more -f largetext.txt
➌ Now, press the ENTER button.
Output >
In the following image, you can see that I have avoided wrapping lines to the next line.
Example 6: Suppressing Multiple Blank Lines Using the “more” Command in Linux
You can squeeze multiple consecutive blank lines into one using the more command in Linux. To get this result you will need to use the option -s. In this example, I will suppress the available blank lines presented in the “largetext.txt” file. In the image below, you can see that the file contains a number of blank lines. To remove such spaces follow the instructions explained further.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more -s largetext.txt
➌ Now, press the ENTER button.
Output >
In the following image, you can see that I have squeezed the multiple spaces into one using the more command.
Example 7: Specifying Line Numbers Per Screen Using the “more” Command in Linux
You can specify the number of lines to be displayed on the terminal using the more command in Linux. To do so you will need to use the option -n. Here, n represents the number of lines you want to view. In this example, I will print 5 lines per screen from the “largetext.txt” file. You can follow the process below to get the same result.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more -5 largetext.txt
➌ Now, press the ENTER button.
Output >
In the below image, you can see that I have shown only 5 lines on the screen from the “largetext.txt” file.
Example 8: Displaying Text After Specified Number of Lines Using the “more” Command in Linux
You can start viewing a file from a certain line number using the more command in Linux with the +number option. It displays contents after the given line number. In this example, I will start showing the contents of the “largetext.txt” after the 5th line.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more +5 largetext.txt
➌ Now, press the ENTER button.
Output >
In the given image, you can see that I have displayed the file contents after its 5th line.
Example 9: Searching for a Pattern Inside a File Using the “more” Command in Linux
You can search for a string inside a file using the more command in Linux with the +/string option. It will start displaying the lines containing the specified string. By pressing the SPACE BAR you can move to the next match of the string. In this example, I will search for the string “code” inside the file “largetext.txt”. To do the same follow the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
more +/code largetext.txt
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I searched and found the string “code” inside my desired file.
Example 10: Redirecting Output to the “more” Command in Linux
You can pipe the output of other commands to be the input of the more command in Linux. In this example, I will display the spaces used by the files in my current directory in the terminal using the more command. You can do the same by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
du -h | more
Note: Here, the du -h command in Linux displays the sizes of files and subdirectories of the current directory in a human-readable format.
➌ Now, press the ENTER button.
Output >
In the following image, you can see that I have displayed the sizes of the files using the more command.
Conclusion
In this article, I have described the application of the more command in Linux with a number of hands-on examples. By learning these applications you will be able to navigate through the file contents from the command line. I hope the experience gained from this article will help you throughout your journey in the Linux environment.
Similar Readings
- The “cat” Command in Linux [10 Practical Examples]
- The “head” Command in Linux [7 Practical Examples]
- The “tail” Command in Linux [7 Practical Examples]
- The “less” Command in Linux [10 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners