Bird
Raised Fist0
Linux CLIscripting~5 mins

less and more (paginated viewing) in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes files or command outputs are too long to fit on one screen. The tools less and more help you view this content page by page, making it easier to read without scrolling endlessly.
When you want to read a long log file without it scrolling past too fast.
When you run a command that outputs many lines and you want to see it bit by bit.
When you need to search inside a long text output interactively.
When you want to pause and resume reading a file at your own pace.
When you want to scroll backward and forward through command output.
Commands
This command opens the syslog file in less, allowing you to scroll through the file page by page. You can move forward with spacebar and backward with 'b'.
Terminal
less /var/log/syslog
Expected OutputExpected
No output (command runs silently)
This command opens the syslog file in more, which also shows the file page by page but has fewer navigation features than less.
Terminal
more /var/log/syslog
Expected OutputExpected
No output (command runs silently)
This pipes the output of dmesg (kernel messages) into less so you can scroll through the messages easily.
Terminal
dmesg | less
Expected OutputExpected
No output (command runs silently)
This pipes the content of syslog through more, showing it page by page. Useful if you want to combine commands with paginated viewing.
Terminal
cat /var/log/syslog | more
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: less is a more powerful pager than more, letting you scroll both forward and backward through long text outputs.

Common Mistakes
Using cat to view a long file without a pager (e.g., cat /var/log/syslog)
The output scrolls past too fast to read, making it hard to see the content.
Use less or more to view the file page by page for easier reading.
Using more when you need to scroll backward
more does not support backward scrolling, so you cannot go back to previous pages.
Use less which supports both forward and backward navigation.
Summary
Use less or more to view long files or command outputs page by page.
less allows both forward and backward scrolling, while more only allows forward.
You can pipe command outputs into less or more to read them easily.

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

  1. 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.
  2. Step 2: Compare with other options

    Options A, B, and D describe deleting, editing, and compiling, which are not functions of less.
  3. Final Answer:

    To view long text files one page at a time with easy scrolling -> Option D
  4. 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

  1. Step 1: Recall basic syntax of more

    The correct way to view a file is by passing the filename as an argument: more filename.
  2. 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.
  3. Final Answer:

    more log.txt -> Option C
  4. 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

  1. Step 1: Understand the command pipeline

    ls -l /etc lists detailed contents of /etc. The pipe | sends this output to less.
  2. Step 2: Effect of less on piped output

    less paginates the output, allowing scrolling through the list one page at a time.
  3. Final Answer:

    Shows the detailed list of /etc directory contents one page at a time with scrolling -> Option A
  4. 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

  1. Step 1: Consider what happens with an empty file

    If file.txt is empty, more has no content to display and exits immediately.
  2. 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.
  3. Final Answer:

    The file file.txt is empty -> Option A
  4. 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

  1. Step 1: Identify commands that allow searching while viewing

    less supports searching inside the file while scrolling up and down.
  2. Step 2: Compare with other commands

    cat shows all content at once; more allows paging but limited searching; head shows only the first lines.
  3. Final Answer:

    less system.log -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard