How to use the command 'more' (with examples)
- Linux
- December 17, 2024
The more
command is a utility in Unix-like operating systems that allows users to view the contents of a file one screen at a time. It is particularly useful for reading large files that do not fit within a single screen and provides basic navigation and search functionalities. While the less
command is often recommended for its enhanced features, more
remains a simple and straightforward tool for interactive file viewing.
Open a File
Code:
more path/to/file
Motivation:
You might have a large file whose contents you want to review in a manageable way. Opening the file with more
lets you read through it screen by screen, which is much more convenient than trying to scroll through the entire content in one long burst.
Explanation:
more
: This invokes themore
command.path/to/file
: This is a placeholder for the actual path to the file you intend to open. This could be any type of textual file, such as a log file, configuration file, or script.
Example Output:
Line 1 of File
Line 2 of File
--More--(12%)
In this example, the content is displayed page by page. The --More--(12%)
prompt signifies that what you see is only 12% of the entire file.
Display a Specific Line
Code:
more +10 path/to/file
Motivation:
If you are only interested in viewing a specific section of a large file, perhaps due to a specific incident noted at that point in a log file, you can jump directly to that line number. This can save time and effort, rather than paging through the content screen by screen until you reach the desired location.
Explanation:
+10
: This argument tellsmore
to start displaying the file beginning at line number 10.path/to/file
: This is the path to the file, which contains the data you want to examine.
Example Output:
Line 10 of File
Line 11 of File
--More--(15%)
Here, the output starts at line 10 and continues, allowing you to quickly focus on the part of the file that is relevant to your needs.
Go to the Next Page
Code:
Space key (while viewing)
Motivation:
When a file’s content exceeds the current screen, you need a way to view the rest of the file. The space bar is a simple way to advance the view to the next section, which is the next page of content. This navigation technique makes it easy to sequentially read through the entire file.
Explanation:
<Space>
: This is the action of pressing the space bar on your keyboard. It signals tomore
to display the next page of the file.
Example Output:
Line 20 of File
Line 21 of File
--More--(25%)
Here, after pressing the space bar, the display has moved forward, showing the next set of lines from the file.
Search for a String
Code:
/something
Motivation:
Searching for specific content within a file makes finding relevant information much more efficient. When dealing with extensive files, like system logs or lengthy reports, using a search function can swiftly guide you to the precise content you need without manually scanning through every line.
Explanation:
/something
: Entering a forward slash (/
) followed by the string you wish to search tellsmore
to look for “something” in the file. If found, it will jump to the line where the string appears.
Example Output:
Line N: ... something ...
--More--(50%)
In this scenario, more
found the string “something” at line N and displayed it, allowing you to continue reading from that point.
Exit
Code:
Press q
(while viewing)
Motivation:
After reviewing the needed content, you’ll eventually want to stop the file viewing session. Exiting is a straightforward action to end the session safely, allowing you to return to the command prompt without traversing through the rest of the file content.
Explanation:
q
: Pressing theq
key quits the currentmore
session.
Example Output:
Shell prompt
Upon pressing q
, the session ends, and you are returned to the shell prompt, ready to execute more commands.
Display Help About Interactive Commands
Code:
Press h
(while viewing)
Motivation:
Understanding all the interactive options available within more
can enhance your user experience. The help menu provides the necessary information to navigate and use the advanced functionality of the more
command which could be beneficial, especially for new users.
Explanation:
h
: By pressing theh
key,more
will display a list of available commands and options that you can use while viewing content.
Example Output:
Commands flagged by * will take a following numerical argument, otherwise any key not in this list is taken as a space.
h: help
q: quit
/<string>: search for a string
...
Here, a list of commands briefly describes how to interact with more
, improving your understanding and efficiency when using the command.
Conclusion:
The more
command remains a necessary and useful tool for file navigation, especially when dealing with large files that don’t conveniently fit on a single screen. From opening files and jumping to specific lines to navigating pages and searching for specific text, more
provides several functionalities that help manage file content more effectively. Understanding these commands can optimize your file reading and management practices significantly.