[Day 2] 7. Redirection
Console Standard Files
The console operates with three internal standard files:
- Standard Input File: Used to take input.
- Standard Output File: Used to display output.
- Standard Error File: Used to display error messages.
These files are collectively referred to as standard files.
There are two ways to give output of one command to another: Redirection and Pipes
Redirection
Redirection allows you to control the flow of input and output in the console. There are three main types of redirection:
-
Input Redirection:
- Instead of taking input from the standard input file, you can take input from another file.
- Operator:
< - Example:
cat < test.txt
-
Output Redirection:
- Instead of writing output to the standard output file, you can write it to another file.
- Operator:
> - Example:
ls > lsout.txt
-
Error Redirection:
- Instead of writing errors to the standard error file, you can write errors to another file.
- Operator:
2> - Example:
command 2> error.txt - You can redirect both standard output and standard error to the same file using:
command > output.txt 2>&1
Pipes
Pipes allow you to connect the output of one command to the input of another. There are two types of pipes:
-
Named Pipe:
- Created using the function.
mkfifo() - Can be used for two logically unrelated processes.
- Created using the
-
Unnamed Pipe:
- Created using the function.
pipe() - Can be used for two related processes.
- Created using the
Note: Both pipes function similarly, but they are used under different circumstances.
!7. Pipe.png
Additional Notes
- The command can be used to perform mathematical operations, and input redirection can also be applied here.
bc - You can combine input and output redirection together, but not all commands support this.
Read Next
[Day 3] 1. Revision
### **Process Control Block (PCB) in Linux** - The `task_struct` is a structure used to create the Process Control Block (PCB). - The size of...
[Day 3] 2. Redirection
### **Redirection in Linux** In Linux, the three standard streams—**stdin** (standard input), **stdout** (standard output), and **stderr** (stand...