Using the "more" Command (with examples)
The “more” command in Unix-like operating systems allows us to open a file for interactive reading, providing features for scrolling and searching. In this article, we will explore several use cases of the “more” command, along with their respective code examples, motivations, and outputs.
1: Open a File
To open a file using the “more” command, simply provide the path to the file as an argument. This will open the file in an interactive mode, allowing you to scroll through its contents.
more path/to/file
Motivation: This use case is useful when you want to quickly view the contents of a file without opening a text editor or other software. It provides a simple and efficient way to inspect the contents of a file directly from the command line.
Example Output:
This is the first line of the file.
This is the second line of the file.
This is the third line of the file.
...
2: Open a File Displaying from a Specific Line
If you want to open a file and start displaying its contents from a specific line, you can use the following command. Replace “line_number” with the desired line number.
more +line_number path/to/file
Motivation: Sometimes, we are only interested in viewing a specific portion of a file. By specifying the starting line, we can skip irrelevant content and quickly navigate to the desired section of the file.
Example Output:
This is the third line of the file.
This is the fourth line of the file.
This is the fifth line of the file.
...
3: Display Help
To display the help information for the “more” command, you can use the “–help” option.
more --help
Motivation: When first learning about a command or when encountering unfamiliar command-line tools, it is essential to have access to help documentation. This use case provides a quick way to access the “more” command’s help information, which can assist in understanding its capabilities and usage.
Example Output:
Usage: more [options] file_name
Options:
-d Display help about interactive commands
-h Display this help message
+<num> Start at line <num>
4: Go to the Next Page
While viewing a file with the “more” command, you can navigate to the next page by pressing the <Space>
key.
Motivation: Some file contents may span multiple pages, making it necessary to scroll through them systematically. This command allows you to conveniently move through the file’s pages, preventing information overload and improving readability.
5: Search for a String
Searching for a specific string within a file can be done during an active “more” session. You can initiate a search by typing ‘/string’ and pressing Enter. To go to the next match, press n
.
Motivation: When dealing with large files or when trying to find specific information within a file, manually scanning through its contents can be time-consuming and inefficient. The search feature in “more” helps locate desired information quickly, saving time and effort.
6: Exit
To exit the “more” session and return to the command prompt, press ‘q’.
Motivation: Once you have finished viewing a file or performing other operations with “more,” closing the session is typically required. This command allows you to cleanly exit the interactive mode, returning to the command-line environment without any hassle.
7: Display Help about Interactive Commands
To access help information about the interactive commands available within “more,” press ‘h’ during a session.
Motivation: The interactive commands provided by “more” can enhance the user’s experience when browsing through a file’s content. Understanding these commands is crucial for efficient navigation and usage of this command-line tool. Accessing the help documentation in real-time ensures that you can self-learn and adapt to the available interactive functionalities.
With the examples, motivations, and explanations provided in this article, you should now have a clear understanding of various use cases of the “more” command. Whether it’s opening a file, starting from a specific line, searching for strings, or accessing help information, the “more” command offers valuable functionality for interactive file reading.