Redirection refers to the output of data to other places, such as devices, files, and so on. That involves the question of where to send the data.
The origin and result of command execution in Linux, see:
Standard input and standard error are output to the screen, which makes the screen very confusing and too much of the output to read. Even sometimes the output content is very important to save, what should I do??
The answer is: data redirection, importing the output to a different location.
Special symbols for data redirection in Linux:
- Standard input: Code 0, using < or <<
- Standard output: Code 1, use 1> or 1>>, or you can use the default representation > or >>
- Standard error Output: Code 2, using 2> or 2>>
The difference between > and >> is that the output content redirects to the destination differently:
To redirect in a way that overwrites the original data of the destination
>>: Re-oriented in an additional way
ls /etc > list
If the above will list the files in the ETC directory, write them to the list file.
- If the list file does not exist, create
- Clear List File contents
- Writes the standard output of LS to the list
If the list is not emptied, it is written in an appended way, using the >>
1. Only the right data is required
Using a/dev/null trash bin
#将find的结果中错误的输出丢弃,只显示正确的结果。避免错误数据的干扰find /home -name .bashrc 2> /dev/null
2. Output data that requires both correct output and bad demand
find /home -name .bashrc > list 2>&1find /home -name .bashrc &> list
Both standard output and standard error are written to list
3. Use of standard input < and <<
Since you are using <, you definitely need to enter the source. < file, you can enter the contents of the file as standard.
cat > back <~/.bashrc
The above uses both > and < (input and output redirection) to back up the. bashrc file.
cat > list
The above command will redirect the screen input (standard input) to the file list as output, and then use Ctrl+d to exit. But you can do the following:
cat > list << "eof"
This allows you to exit as long as "EOF" is entered on the screen. << represents end input.
Data redirection in Linux