[Day 3] 2. Redirection
Redirection in Linux
In Linux, the three standard streams—stdin (standard input), stdout (standard output), and stderr (standard error)—can be redirected. Redirection alters the flow of input/output for commands.
Basic Commands:
-
: Returns the number of lines, word count, and character count in a file.
wcExample:
wc < test.txt
This command takes input from the fileinstead of standard input (stdin), demonstrating input redirection.test.txt
!2. Redirection.png
Input and Output Redirection:
wc < test.txt > output.txt
This command demonstrates both input and output redirection. Input is taken from, and the result is redirected to the filetest.txt.output.txt
Error Redirection and Merging:
wc -p < test.txt > output.txt 2>&1
This merges stderr (error stream) with stdout, whererefers to2andstderrrefers to1. The command means that both output and errors are written intostdout.output.txt
How Redirection works?
-
Create a FIFO file
mkfifo fifo- This creates a named pipe (FIFO file) called . It's a special file that acts as a temporary buffer for data between processes, ensuring that data is read in the same order it was written.
fifo
- This creates a named pipe (FIFO file) called
-
Sorting numbers from
and writing them intonumbers.txtfifosort numbers.txt > fifo- The command sorts the numbers in the file
sort numbers.txt.numbers.txt - The symbol redirects the sorted output to the FIFO file
>, instead of displaying it on the terminal (which is the default behavior offifo).stdout
- The command
-
Reading from
usingfifoand removing duplicatesuniquniq < fifo > output.txt- Here, the input redirection () reads the sorted data from the FIFO file
< fifoand passes it to thefifocommand.uniq - The command removes duplicate entries from the sorted list.
uniq - The symbol redirects the final output (unique sorted numbers) to a file called
>.output.txt
- Here, the input redirection (
!2. Output Redirection.png
How Redirection Works Internally:
-
Input Redirection (
):<- For , the
uniq < fifocommand reads its input fromuniq, which contains the sorted data from the previous step. This replaces the default stdin (keyboard input) withfifo.fifo
- For
-
Output Redirection (
):>- In both and
sort numbers.txt > fifo, the output is redirected from the terminal (the default stdout) to either a FIFO file or an output file (uniq < fifo > output.txt).output.txt - The default output stream (, file descriptor
stdout) is closed, and the new file (1orfifo) is assigned a file descriptor.output.txt
- In both
-
FIFO (First In, First Out):
- A FIFO acts like a buffer between two processes (sort and uniq in this case). One process writes to it (), while another process reads from it (
sort). The data is processed in the same order it was written.uniq
- A FIFO acts like a buffer between two processes (sort and uniq in this case). One process writes to it (
Summary of Redirection in the Example:
- You sort the contents of and redirect the sorted output to the FIFO (
numbers.txt).sort numbers.txt > fifo - Then, you read the sorted data from using input redirection (
fifo) and pass it to< fifo, which removes the duplicates and redirects the unique data intouniq.output.txt
This is how redirection works, allowing you to process data without pipes, instead relying on explicit redirection with FIFO files.
Read Next
[Day 3] 3. Pipes
### **Pipes in Linux** Pipes are used for **Inter-Process Communication (IPC)**, allowing data to flow between processes. They have two ends: one f...
[Day 3] 4. Some more commands
#### **1. Head and Tail Commands:** - **`head`**: Displays the first part of files. `head -5 filename.txt` - This command pr...