Using less and more for Paginated Viewing in Linux CLI
📖 Scenario: You are working on a Linux server and need to read long text files without scrolling endlessly. Using paginated viewing tools like less and more helps you read files page by page, making it easier to find information.
🎯 Goal: Learn how to use less and more commands to view the contents of a text file one page at a time.
📋 What You'll Learn
Create a text file named sample.txt with specific content.
Use a variable to store the filename.
Use the more command to view the file content paginated.
Use the less command to view the file content paginated.
💡 Why This Matters
🌍 Real World
Viewing long log files, configuration files, or command outputs in a manageable way on Linux servers or terminals.
💼 Career
System administrators, developers, and anyone working with Linux command line need to efficiently read and navigate large text files.
Progress0 / 4 steps
1
Create a text file with sample content
Create a file named sample.txt with the following exact content (each line): Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10
Linux CLI
Hint
Use the echo command with -e to add new lines and redirect output to sample.txt.
2
Store the filename in a variable
Create a variable called filename and assign it the value sample.txt.
Linux CLI
Hint
Assign the string sample.txt to the variable filename without spaces around the equals sign.
3
View the file content using more
Use the more command with the variable filename to view the file content page by page.
Linux CLI
Hint
Use more "$filename" to view the file content stored in the variable.
4
View the file content using less
Use the less command with the variable filename to view the file content page by page.
Linux CLI
Hint
Use less "$filename" to view the file content stored in the variable.
Practice
(1/5)
1. What is the main purpose of the less command in Linux?
easy
A. To delete files from the system
B. To edit text files directly in the terminal
C. To compile source code files
D. To view long text files one page at a time with easy scrolling
Solution
Step 1: Understand the function of less
less is a pager program used to view text files or command output one page at a time.
Step 2: Compare with other options
Options A, B, and D describe deleting, editing, and compiling, which are not functions of less.
Final Answer:
To view long text files one page at a time with easy scrolling -> Option D
Quick Check:
less = paginated viewing [OK]
Hint: Remember: less lets you scroll up and down easily [OK]
Common Mistakes:
Confusing less with text editors like vi
Thinking less edits files
Assuming less deletes files
2. Which of the following is the correct syntax to view a file named log.txt using more?
easy
A. more > log.txt
B. more log.txt <
C. more log.txt
D. more log.txt -f
Solution
Step 1: Recall basic syntax of more
The correct way to view a file is by passing the filename as an argument: more filename.
Step 2: Analyze options
more log.txt < uses incomplete input redirection syntax; more > log.txt uses output redirection which is incorrect; more log.txt -f places option after filename, treating -f as a file.
Final Answer:
more log.txt -> Option C
Quick Check:
Use more filename to view files [OK]
Hint: Use more filename to open files quickly [OK]
Common Mistakes:
Using output redirection instead of input
Adding invalid options to more
Confusing input and output redirection
3. What will be the output when you run the command ls -l /etc | less?
medium
A. Shows the detailed list of /etc directory contents one page at a time with scrolling
B. Shows an error because less cannot be used with pipes
C. Deletes the /etc directory contents
D. Lists the contents of /etc directory all at once without paging
Solution
Step 1: Understand the command pipeline
ls -l /etc lists detailed contents of /etc. The pipe | sends this output to less.
Step 2: Effect of less on piped output
less paginates the output, allowing scrolling through the list one page at a time.
Final Answer:
Shows the detailed list of /etc directory contents one page at a time with scrolling -> Option A
Quick Check:
Piping to less = paginated output [OK]
Hint: Pipe output to less to scroll long lists easily [OK]
Common Mistakes:
Thinking less deletes files
Assuming less shows all output at once
Believing less cannot be used with pipes
4. You run more file.txt but the command exits immediately without showing the file content. What is the most likely reason?
medium
A. The file file.txt is empty
B. You typed more incorrectly
C. The terminal does not support paging
D. The file file.txt is a directory
Solution
Step 1: Consider what happens with an empty file
If file.txt is empty, more has no content to display and exits immediately.
Step 2: Evaluate other options
Typing errors would cause command not found or syntax errors; terminal support is rarely an issue; if file.txt is a directory, more would show an error.
Final Answer:
The file file.txt is empty -> Option A
Quick Check:
Empty file = no output from more [OK]
Hint: Empty files cause more to exit immediately [OK]
Common Mistakes:
Assuming more always shows something
Not checking if file is empty
Confusing file with directory
5. You want to view a log file system.log but also want to search inside it while scrolling. Which command is best to use?
hard
A. more system.log
B. less system.log
C. cat system.log
D. head system.log
Solution
Step 1: Identify commands that allow searching while viewing
less supports searching inside the file while scrolling up and down.
Step 2: Compare with other commands
cat shows all content at once; more allows paging but limited searching; head shows only the first lines.
Final Answer:
less system.log -> Option B
Quick Check:
Use less for scrolling and searching [OK]
Hint: Use less to scroll and search inside files [OK]
Common Mistakes:
Using cat for long files
Choosing more which has limited search
Using head which shows only start
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.