Warm Reminder
If you like this article, please share it with your friends. If you have any questions or want more information, please follow or leave a message.
In Linux systems, viewing file contents is an essential part of daily management tasks. For large files, trying to view all content at once can result in a terminal displaying a lot of information, making it hard to read.<span><span>more</span></span> command is a commonly used tool for paginated viewing of file contents, providing an interactive browsing feature that helps users efficiently view file contents. This article will delve into the common parameters and functions of the <span>more</span> command, with detailed explanations based on practical examples.
Viewing File Contents
File content viewing operations refer to displaying the contents of a file. In Linux, the <span>more</span> command is used for paginated viewing of file contents, allowing users to browse file contents page by page.
<span><span>more</span></span> Command and Its Parameters Explained
-
Basic Syntax
-
<span>more [options] filename</span> -
<span>more</span>command is used for paginated viewing of file contents.
Parameter Details
-
<span>-num</span>: Sets the initial number of lines to display. -
<span>-d</span>: Displays help information at the bottom, prompting users with available key operations. -
<span>-l</span>: Does not interpret newline characters in the file contents as new lines, suitable for viewing files with long lines. -
<span>-f</span>: Interprets newline characters in the file contents as new lines, even if the line length exceeds the terminal width. -
<span>-p</span>: Clears the screen before displaying each page of content. -
<span>-c</span>: Clears the screen before displaying each page of content, similar to<span>-p</span>. -
<span>-s</span>: Compresses multiple consecutive blank lines into one line for display. -
<span>-u</span>: Does not convert tabs in the file contents to spaces. -
<span>/pattern</span>: Searches for lines matching<span>pattern</span>in the file contents and starts displaying from that line. -
<span>+num</span>: Starts displaying from the<span>num</span>line of the file. -
<span>+cmd</span>: Starts displaying file contents after executing the<span>cmd</span>command, where<span>cmd</span>can be any internal command supported by<span>more</span>.
Code Examples
# Create a test file
echo "Line 1" > test_file.txt
echo "Line 2" >> test_file.txt
echo "Line 3" >> test_file.txt
echo "Line 4" >> test_file.txt
echo "Line 5" >> test_file.txt
echo "Line 6" >> test_file.txt
echo "Line 7" >> test_file.txt
echo "Line 8" >> test_file.txt
echo "Line 9" >> test_file.txt
echo "Line 10" >> test_file.txt
# Use more to view file contents
more test_file.txt
# Start displaying from line 5
more +5 test_file.txt
# Search for lines containing "Line" in the file
more /Line test_file.txt
# Display help information
more -d test_file.txt

Explanation of Code Examples
1. View File Contents
-
<span>more test_file.txt</span>: Paginated display of the contents of<span>test_file.txt</span>. Press the<span>Space</span>key to page down, the<span>b</span>key to go back a page, and the<span>q</span>key to exit.
2. Start Displaying from a Specific Line
-
<span>more +5 test_file.txt</span>: Starts displaying content from the 5th line of<span>test_file.txt</span>.
3. Search for More Specific Content
-
<span>more /Line test_file.txt</span>: Searches and displays lines in<span>test_file.txt</span>that contain “Line”.
4. Display Help Information
-
<span>more -d test_file.txt</span>: Paginated display of the contents of<span>test_file.txt</span>, with help information displayed at the bottom, prompting users with available key operations.
Advantages
-
Highly Interactive: The
<span>more</span>command provides an interactive browsing feature, allowing users to view file contents page by page, suitable for handling large files. -
Simple and Easy to Use: The syntax of the
<span>more</span>command is simple and easy to learn, suitable for quickly viewing file contents. -
Search Functionality: Supports searching for specific strings in file contents, making it easy to quickly locate content.
Disadvantages
-
Relatively Limited Functionality: Compared to the
<span>less</span>command, the<span>more</span>command has limited functionality, such as not being able to search backward or scroll. -
Does Not Support Editing: The
<span>more</span>command can only be used to view file contents, not to edit files.
Exercises
Exercise 1: Use the <span>more</span> command to view the contents of a file.
Exercise 2: Use the <span>more +num</span> command to start viewing file contents from a specified line.
Exercise 3: Use the <span>more /pattern</span> command to search for specific content in the file.
Exercise 4: Use the <span>more -d</span> command to view file contents and display help information.
Exercise 5: In the interactive mode of the <span>more</span> command, use the <span>Space</span> key, <span>b</span> key, and <span>q</span> key to perform page turning, going back, and exiting operations.
Exercise Answers and Hints
Exercise 1: Run <span>more filename.txt</span> to view the contents of the file <span>filename.txt</span>.
Exercise 2: Run <span>more +5 filename.txt</span> to start displaying content from the 5th line of the file <span>filename.txt</span>.
Exercise 3: Run <span>more /pattern filename.txt</span> to search and display lines in the file <span>filename.txt</span> that contain <span>pattern</span>.
Exercise 4: Run <span>more -d filename.txt</span> to view the contents of the file <span>filename.txt</span>, with help information displayed at the bottom.
Exercise 5: In the interactive mode of the <span>more</span> command, press the <span>Space</span> key to page down, the <span>b</span> key to go back a page, and the <span>q</span> key to exit.
Through this case study, you have mastered various parameters of the <span>more</span> command and its application scenarios, enabling you to more efficiently view file contents in the file system.
