Redirection Operators (>,>>,<,error 2>)
Types of Redirection
1. Overwrite
- “>” standard output
- “<” standard input
2. Appends
- “>>” standard output
- “<<” standard input
3. Merge
- “p >& q” Merges output from stream p with stream q
- “p <& q” Merges input from stream p with stream q
EXAMPLES
Regular output > operator
[damon@localhost ~]$ date
Output--->>Tue Dec 29 04:07:37 PM MST 2020
[damon@localhost ~]$ date > specifications.txt
[damon@localhost ~]$ cat specifications.txt
Output--->>Tue Dec 29 04:08:44 PM MST 2020
Desc------>> Date function data will store directly to specifications.txt file by using (>) Operator
Note------>>The problem with the > redirector is that it overwrites any existing data in the file
ex:
[damon@localhost ~]$ hostname > specifications.txt
[damon@localhost ~]$ cat specifications.txt
Output--->> localhost.localdomain
Desc------>> Overwrited the date data to hostname data
Regular output append >> operator
[damon@localhost ~]$ date > specifications.txt
[damon@localhost ~]$ hostname >> specifications.txt
[damon@localhost ~]$ uname -r >> specifications.txt
[damon@localhost ~]$ cat specifications.txt
Output--->>Tue Dec 29 04:11:51 PM MST 2020
localhost.localdomain
5.9.16-200.fc33.x86_64
Desc ------>> (append >>) operator adds the output to the existing content instead of overwriting it.
Regular input < operator
[damon@localhost ~]$ sort < mylist.txt
Output--->>cat
cow
dog
horse
Desc ------>> You can pull the contents of the file into the sort command by using the < operator.
[damon@localhost ~]$ sort < mylist.txt > alphabetical-file.txt
[damon@localhost ~]$ cat alphabetical-file.txt
Output--->>cat
cow
dog
horse
Desc ------>> redirect the sorted results to a new file:
Regular error 2> operator
Desc ------>> When a program or script does not generate the expected results, it throws an error. The error is usually sent to the stdout, but it can be redirected elsewhere. The stderr operator is 2> (for file descriptor 2).
------------------------------------------------------------------------
File File Descriptor
------------------------------------------------------------------------
Standard Input STDIN 0
Standard Output STDOUT 1
Standard Error STDERR 2
------------------------------------------------------------------------
myprogram 2>errorfile
Desc ------>> The file descriptor for standard error is 2.
Using “2>” we re-direct the error output to a file named “errorfile”
Thus, program output is not cluttered with errors.