File Editing, Redirection
File Editing, Redirection
> file 2>&1 redirect stdout and stderr to overwrite the same file
The following sequence redirects standard output to file and then redirects
standard error to the same place as standard output (file).
&> file
>> file 2>&1 redirect stdout and stderr to append to the same file
&>> file
This redirects standard error to the default place for standard output (the
terminal window, so no change) and then redirects only standard output to
file.
Copy the last 100 lines from a log file to another file.
List the home directory's hidden and regular file names into a file.
[user@host ~]$ find /etc -name passwd > /tmp/output 2> /tmp/errors
[user@host ~]$ find /etc -name passwd > /tmp/output 2> /dev/null
This example takes the output of the ls command and uses less to display it on the terminal
one screen at a time.
The tee command overcomes this limitation. In a pipeline, tee copies its standard input to its
standard output and also redirects its standard output to the files named as arguments to the
command.
This example redirects the output of the ls command to the file and passes it to less to be
displayed on the terminal one screen at a time.