Bird
Raised Fist0
Linux CLIscripting~10 mins

less and more (paginated viewing) in Linux CLI - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the command to view the contents of a file named 'example.txt' one page at a time using the 'less' command.

Linux CLI
less [1]
Drag options to blanks, or click blank then click option'
A-n
B-r
C--help
Dexample.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Using options like '-r' or '-n' without specifying the file.
Typing only 'less' without a file name.
2fill in blank
medium

Complete the command to view the output of 'dmesg' command one page at a time using 'more'.

Linux CLI
dmesg | [1]
Drag options to blanks, or click blank then click option'
Aless
Bmore
Ccat
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cat' which outputs all at once.
Using 'head' which shows only the first few lines.
3fill in blank
hard

Fix the error in the command to view a file named 'log.txt' with 'less' and show line numbers.

Linux CLI
less [1] log.txt
Drag options to blanks, or click blank then click option'
A-N
B-n
C-l
D-h
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-n' which disables line numbers in 'less'.
Using '-l' or '-h' which are unrelated options.
4fill in blank
hard

Fill both blanks to create a command that shows the first 20 lines of 'data.txt' and then views it page by page.

Linux CLI
head [1] [2] data.txt | less
Drag options to blanks, or click blank then click option'
A-n
Bdata.txt
C20
D-20
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-20' instead of '-n 20' which is less clear.
Swapping the order of options and filename.
5fill in blank
hard

Fill all three blanks to create a command that searches for the word 'error' in 'system.log', ignoring case, and views the results page by page.

Linux CLI
grep [1] [2] [3] | less
Drag options to blanks, or click blank then click option'
A-i
Berror
Csystem.log
D-v
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-v' which inverts the match.
Swapping the order of search word and filename.

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