Linux shells use three standard I/O streams, each of which is
associated with a well-known file descriptor:
Redirecting output
Output redirection using n> usually overwrites existing files. You can control this with the noclobber option of the set builtin.
This will redirect all errors to standard output and standard output is redirected to the file. The errors will be displayed in the terminal and the output is written to a file.
This next example first redirects all standard output to a file and then redirects errors to the standard output. This causes all content to be written to the file.
Replaces letter a in the entries of the redirected input file with letter b.
Creating Pipelines
You use the | (pipe) operator between two commands to direct the stdout of the first to the stdin of the second.
Redirected errors and output lines will be passed to be sorted.
Contents of file 3 will be passed to the tr command which is redirected to a new file called file4.
The hyphen is used in place of file name to be redirected from the standard input. This will wait until the standard input becomes available.
xargs Command
args > 1 apple 2 pear 3 banana
args > pear 3 banana
teeCommand
Displays results in the screen while writing to a file
- stdout is the standard output stream, which displays output from commands. It has file descriptor 1.
- stderr is the standard error stream, which displays error output from commands. It has file descriptor 2.
- stdin is the standard input stream, which provides input to commands. It has file descriptor 0.
Redirecting output
ls x* z* >stdout.txt 2>stderr.txt
Standard output in the first file and the errors in the second file.
Output redirection using n> usually overwrites existing files. You can control this with the noclobber option of the set builtin.
ls x* z* 2>&1 >output.txt
This will redirect all errors to standard output and standard output is redirected to the file. The errors will be displayed in the terminal and the output is written to a file.
ls x* z* >output.txt 2>&1
This next example first redirects all standard output to a file and then redirects errors to the standard output. This causes all content to be written to the file.
ls x* z* &>output.txt
Redirects both outputs to the file
ls x* z* 2>/dev/null
Ignores errors by writing to a null file.
tr a b > text1
Replaces letter a in the entries of the redirected input file with letter b.
Creating Pipelines
You use the | (pipe) operator between two commands to direct the stdout of the first to the stdin of the second.
ls y* x* z* u* q* 2>&1 |sort
Redirected errors and output lines will be passed to be sorted.
cat file3 | tr c d > file4
Contents of file 3 will be passed to the tr command which is redirected to a new file called file4.
bunzip2 -c somefile.tar.bz2 | tar -xvf -
The hyphen is used in place of file name to be redirected from the standard input. This will wait until the standard input becomes available.
xargs Command
xargs < text1 echo "args>"
args > 1 apple 2 pear 3 banana
xargs --max-args 3 text1 echo "args>"args > 1 apple 2
teeCommand
ls tee | file.txt
Displays results in the screen while writing to a file
Comments
Post a Comment