📖 Scenario: You have a text file with a list of names. You want to use a command that reads from this file as if you typed the names directly into the command.
🎯 Goal: Learn how to use stdin redirection (
📋 What You'll Learn
Create a text file named names.txt with specific names.
Use stdin redirection (<) to pass the file content to the cat command.
Display the content of the file using the command with stdin redirection.
💡 Why This Matters
🌍 Real World
Many Linux commands can read input from files using stdin redirection. This helps automate tasks without typing input manually.
💼 Career
Understanding stdin redirection is essential for system administrators, developers, and anyone working with command-line tools to automate workflows.
Progress0 / 4 steps
1
Create the names.txt file
Create a file called names.txt with these exact three lines: Alice, Bob, and Charlie.
Linux CLI
Hint
Use the echo command with -e to add new lines and redirect output to names.txt.
2
Prepare to use stdin redirection
Use the cat command with stdin redirection (<) to read from the file names.txt. Write the command without running it.
Linux CLI
Hint
Use cat < names.txt to read the file content through stdin redirection.
3
Run the command with stdin redirection
Run the command cat < names.txt to display the content of names.txt using stdin redirection.
Linux CLI
Hint
Just run the command cat < names.txt and observe the output.
4
Confirm understanding by printing a message
Print the message File content displayed using stdin redirection after showing the file content.
Linux CLI
Hint
Use printf or echo to print the message after the file content.
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
Step 1: Understand stdin redirection with <
The < symbol sends the content of names.txt as input to the sort command.
Step 2: Effect of the command
The sort command reads the file content and outputs the sorted lines to the screen (standard output).
Final Answer:
It sorts the lines inside the file names.txt and shows the result. -> Option A
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
Step 1: Identify correct stdin redirection syntax
The correct way to redirect input is command < filename. So wc -l < input.txt is 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 D
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
Step 1: Understand the input file content
The file numbers.txt has three lines: 3, 1, and 2.
Step 2: Effect of sort < numbers.txt
The sort command reads the lines and outputs them sorted ascending: 1, 2, 3.
Final Answer:
1
2
3 -> Option A
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
Step 1: Check if input redirection is valid for grep
grep supports reading from stdin, so grep 'apple' < fruits.txt is valid syntax.
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'.
Final Answer:
The pattern 'apple' is case-sensitive and does not match the file content. -> Option C
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
Step 1: Use grep with case-insensitive option and stdin redirection