Bird
Raised Fist0
Linux CLIscripting~20 mins

stdin redirection (<) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stdin Redirection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this command using stdin redirection?
Given a file named numbers.txt containing:
1
2
3
What is the output of the command cat < numbers.txt?
A1 2 3
Bnumbers.txt
Ccat: numbers.txt: No such file or directory
D1\n2\n3
Attempts:
2 left
💡 Hint
Think about what cat does when reading from standard input.
💻 Command Output
intermediate
1:30remaining
What error occurs when redirecting stdin from a non-existent file?
What happens when you run sort < missingfile.txt if missingfile.txt does not exist?
Abash: missingfile.txt: No such file or directory
BNo output, command waits for input
Csort: standard input: No such file or directory
Dsort: cannot open missingfile.txt for reading: Permission denied
Attempts:
2 left
💡 Hint
Consider how the shell handles input redirection from a file that does not exist.
📝 Syntax
advanced
1:30remaining
Which command correctly uses stdin redirection to count lines in a file?
You want to count the number of lines in data.txt using wc -l and stdin redirection. Which command is correct?
Awc -l < data.txt
Bwc < -l data.txt
Cwc -l data.txt <
D< wc -l data.txt
Attempts:
2 left
💡 Hint
Remember the syntax for input redirection: command < filename
🚀 Application
advanced
2:00remaining
How to use stdin redirection with a script that reads input?
You have a script process.sh that reads lines from standard input. Which command correctly feeds input.txt to the script using stdin redirection?
A./process.sh input.txt
Bcat input.txt | ./process.sh
C./process.sh < input.txt
D./process.sh > input.txt
Attempts:
2 left
💡 Hint
Think about how stdin redirection works versus passing a filename as an argument.
🧠 Conceptual
expert
2:30remaining
What happens if you combine stdin redirection with a command that also reads a file argument?
Consider the command: grep 'error' < log.txt log2.txt
What will happen?
Agrep searches 'error' in log2.txt and also reads log.txt from stdin
Bgrep searches 'error' only in log2.txt, ignoring stdin
Cgrep searches 'error' in log2.txt and ignores stdin redirection
Dgrep searches 'error' in log.txt twice, once from stdin and once from log2.txt
Attempts:
2 left
💡 Hint
Think about how stdin redirection and file arguments work together in commands.

Practice

(1/5)
1. What does the command sort < names.txt do in Linux?
easy
A. It sorts the lines inside the file names.txt and shows the result.
B. It saves the sorted output into names.txt.
C. It renames the file names.txt to sort.
D. It deletes the file names.txt after sorting.

Solution

  1. Step 1: Understand stdin redirection with <

    The < symbol sends the content of names.txt as input to the sort command.
  2. Step 2: Effect of the command

    The sort command reads the file content and outputs the sorted lines to the screen (standard output).
  3. Final Answer:

    It sorts the lines inside the file names.txt and shows the result. -> Option A
  4. Quick Check:

    stdin redirection < sends file content as input [OK]
Hint: Remember: < sends file content as input to command [OK]
Common Mistakes:
  • Thinking < saves output to the file
  • Confusing < with > (output redirection)
  • Assuming the file is modified
2. Which of the following is the correct syntax to use stdin redirection to provide input from a file named input.txt to the command wc -l?
easy
A. < wc -l input.txt
B. wc -l > input.txt
C. wc -l input.txt <
D. wc -l < input.txt

Solution

  1. Step 1: Identify correct stdin redirection syntax

    The correct way to redirect input is command < filename. So wc -l < input.txt is correct.
  2. Step 2: Check other options

    wc -l > input.txt uses > which is output redirection. Options C and D have incorrect order or syntax.
  3. Final Answer:

    wc -l < input.txt -> Option D
  4. Quick Check:

    stdin redirection syntax = command < file [OK]
Hint: Use < between command and filename for input redirection [OK]
Common Mistakes:
  • Using > instead of < for input
  • Placing < after filename
  • Starting command line with <
3. Given a file numbers.txt containing the lines:
3
1
2

What is the output of the command sort < numbers.txt?
medium
A. 1 2 3
B. 3 1 2
C. 2 3 1
D. Syntax error

Solution

  1. Step 1: Understand the input file content

    The file numbers.txt has three lines: 3, 1, and 2.
  2. Step 2: Effect of sort < numbers.txt

    The sort command reads the lines and outputs them sorted ascending: 1, 2, 3.
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    sort command sorts lines from stdin [OK]
Hint: Sort reads from stdin when using < redirection [OK]
Common Mistakes:
  • Assuming output is original order
  • Confusing input and output redirection
  • Expecting numbers sorted numerically without options
4. You run the command grep 'apple' < fruits.txt but get no output, even though fruits.txt contains lines with 'apple'. What is the most likely error?
medium
A. The file fruits.txt is empty.
B. You used output redirection instead of input redirection.
C. The pattern 'apple' is case-sensitive and does not match the file content.
D. The command syntax is incorrect; grep does not support input redirection.

Solution

  1. Step 1: Check if input redirection is valid for grep

    grep supports reading from stdin, so grep 'apple' < fruits.txt is valid syntax.
  2. Step 2: Consider case sensitivity of grep

    By default, grep is case-sensitive. If the file has 'Apple' or 'APPLE', it won't match 'apple'.
  3. Final Answer:

    The pattern 'apple' is case-sensitive and does not match the file content. -> Option C
  4. Quick Check:

    grep is case-sensitive by default [OK]
Hint: Check case sensitivity when grep finds no matches [OK]
Common Mistakes:
  • Assuming input redirection is wrong for grep
  • Ignoring case differences in pattern
  • Thinking file is empty without checking
5. You want to count how many lines in data.txt contain the word 'error' ignoring case, using stdin redirection. Which command achieves this?
hard
A. wc -l < grep -i 'error' data.txt
B. grep -i 'error' < data.txt | wc -l
C. grep 'error' > data.txt | wc -l
D. grep -i 'error' > data.txt | wc -l

Solution

  1. Step 1: Use grep with case-insensitive option and stdin redirection

    grep -i 'error' < data.txt reads data.txt ignoring case.
  2. Step 2: Pipe output to wc -l to count matching lines

    Adding | wc -l counts the number of lines output by grep.
  3. Final Answer:

    grep -i 'error' < data.txt | wc -l -> Option B
  4. Quick Check:

    Use grep -i with < and pipe to wc -l [OK]
Hint: Pipe grep output to wc -l for counting lines [OK]
Common Mistakes:
  • Using > instead of < for input
  • Placing wc -l before grep
  • Redirecting output to input file mistakenly
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