stdin redirection (<) in Linux CLI - Time & Space Complexity
We want to understand how the time taken by a command changes when we use stdin redirection.
Specifically, how does reading input from a file affect execution time as the file size grows?
Analyze the time complexity of the following command using stdin redirection.
sort < input.txt
This command sorts lines from the file input.txt by reading it through stdin redirection.
Look at what repeats as the input grows.
- Primary operation: Reading each line from the input file and sorting all lines.
- How many times: Once for each line in the file, plus sorting steps that depend on total lines.
As the file gets bigger, the command reads more lines and sorting takes longer.
| Input Size (n lines) | Approx. Operations |
|---|---|
| 10 | Reads 10 lines, sorts 10 lines |
| 100 | Reads 100 lines, sorts 100 lines |
| 1000 | Reads 1000 lines, sorts 1000 lines |
Pattern observation: Reading grows linearly, sorting grows faster than linear as input grows.
Time Complexity: O(n log n)
This means the time grows a bit faster than the number of lines because sorting takes more steps as input grows.
[X] Wrong: "Reading input with < is instant and does not affect time."
[OK] Correct: Reading input still takes time proportional to file size, so bigger files take longer to read before sorting.
Understanding how input size affects command time helps you reason about script performance and efficiency in real tasks.
What if we replaced sort < input.txt with sort input.txt? How would the time complexity change?
Practice
sort < names.txt do in Linux?Solution
Step 1: Understand stdin redirection with
The<<symbol sends the content ofnames.txtas input to thesortcommand.Step 2: Effect of the command
Thesortcommand reads the file content and outputs the sorted lines to the screen (standard output).Final Answer:
It sorts the lines inside the filenames.txtand shows the result. -> Option AQuick Check:
stdin redirection<sends file content as input [OK]
- Thinking < saves output to the file
- Confusing < with > (output redirection)
- Assuming the file is modified
input.txt to the command wc -l?Solution
Step 1: Identify correct stdin redirection syntax
The correct way to redirect input iscommand < filename. Sowc -l < input.txtis correct.Step 2: Check other options
wc -l > input.txt uses > which is output redirection. Options C and D have incorrect order or syntax.Final Answer:
wc -l < input.txt -> Option DQuick Check:
stdin redirection syntax = command < file [OK]
- Using > instead of < for input
- Placing < after filename
- Starting command line with <
numbers.txt containing the lines:3 1 2
What is the output of the command
sort < numbers.txt?Solution
Step 1: Understand the input file content
The filenumbers.txthas three lines: 3, 1, and 2.Step 2: Effect of
Thesort < numbers.txtsortcommand reads the lines and outputs them sorted ascending: 1, 2, 3.Final Answer:
1 2 3 -> Option AQuick Check:
sort command sorts lines from stdin [OK]
- Assuming output is original order
- Confusing input and output redirection
- Expecting numbers sorted numerically without options
grep 'apple' < fruits.txt but get no output, even though fruits.txt contains lines with 'apple'. What is the most likely error?Solution
Step 1: Check if input redirection is valid for grep
grepsupports reading from stdin, sogrep 'apple' < fruits.txtis valid syntax.Step 2: Consider case sensitivity of grep
By default,grepis case-sensitive. If the file has 'Apple' or 'APPLE', it won't match 'apple'.Final Answer:
The pattern 'apple' is case-sensitive and does not match the file content. -> Option CQuick Check:
grep is case-sensitive by default [OK]
- Assuming input redirection is wrong for grep
- Ignoring case differences in pattern
- Thinking file is empty without checking
data.txt contain the word 'error' ignoring case, using stdin redirection. Which command achieves this?Solution
Step 1: Use grep with case-insensitive option and stdin redirection
grep -i 'error' < data.txtreadsdata.txtignoring case.Step 2: Pipe output to
Addingwc -lto count matching lines| wc -lcounts the number of lines output by grep.Final Answer:
grep -i 'error' < data.txt | wc -l -> Option BQuick Check:
Usegrep -iwith<and pipe towc -l[OK]
- Using > instead of < for input
- Placing wc -l before grep
- Redirecting output to input file mistakenly
Ready to prove it?
Pass the test and this pattern moves unknown
