Redirection operator in UNIX
Suppose I have three files file1 file2 file3 having some content
Now when I do this on shell prompt cat file1 > file2 >file3
Content of file1 is copied to file3 and file2 becomes empty
Similarly when I do cat > file1 > file2 > file3
It ask for input and this input is stored in file3 and both fi开发者_开发问答le1 and file2 are empty
and also for cat > file1 > file2 < file3
contents of file3 is copied to file2 and file1 is empty.
Can someone please explain to me what is happening I am new to UNIX. Also any website where I can learn more about these redirection operators.
Thanks
Consider how the shell processes each part of the command as it parses it:
cat file1 > file2 >file3
cat file1
: prepare a new process with thecat
program image with argumentfile1
. ( given 1 or more arguments,cat
will read from each argument as a file and write to its output file descriptor)> file2
: change the new process' output file descriptor to write tofile2
instead of the current output sink (initially the console for an interactive shell) - create `file2 if necessary.> file3
: change the new process' output file descriptor to write tofile3
instead of the current output sink (wasfile2
) - createfile3
if necessaryEnd of command
: Spawn the new process
So in the end, file2
is created, but unused. file3
gets the data.
cat > file1 > file2 > file3
cat
: prepare a new process with thecat
program/image with no arguments. (given no arguments,cat
will read from its input file descriptor and write to its output file descriptor)> file1
: change the new process' output file descriptor to write tofile1
instead of the current output sink (initially the console for an interactive shell) - createfile1
if necessary.> file2
: change the new process' output file descriptor to write tofile2
instead of the current output sink (wasfile1
) - createfile2
if necessary.> file3
: change the new process' output file descriptor to write tofile3
instead of the current output sink - (wasfile2
) createfile3
if necessaryEnd of command
: Spawn the new process
So in the end, file1
and file2
are created, but unused. file3
gets the data. cat
waits for input on its input device (the console device as default for an interactive shell). Any input that cat
receives will go to its output device (which ends up being file3
by the time the shell finished processing the command and invoked cat
).
cat > file1 > file2 < file3
cat
: prepare a new process with thecat
program/image with no arguments. (given no arguments,cat
will read from its input file descriptor and write to its output file descriptor)> file1
: change the new process' output file descriptor to write tofile1
instead of the current output sink (initially the console for an interactive shell) - createfile1
if necessary.> file2
: change the new process' output file descriptor to write tofile2
instead of the current output sink (wasfile1
) - createfile2
if necessary.< file3
: change the new process' input file descriptor to read fromfile3
instead of the current input source (initially the console for an interactive shell)End of command
: Spawn the new process
So in the end, file1
is created, but unused. file2
gets the data. cat
waits for input on its input device (which as set to file3
by the time the shell finished processing the command and invoked cat
). Any input that cat
receives will go to its output device (which ends up being file2
by the time the shell finished processing the command and invoked cat
).
--
Note that in the first example, cat
is the one who processes/opens file1
. The shell simply passed the word file1
to the program as an argument. However, the shell opened/created file2
and file3
. cat
knew nothing about file3
and has no idea where the stuff it was writing to its standard output was going.
In the other 2 examples, the shell opened all the files. cat
knew nothing about any files. cat
had no idea where its standard input was coming from and where its standard output was going to.
Per @Sorpigal comment - the BASH manual has some good descriptions of what the different redirection operators do. Much of it is the same across different Unix shells to varying degrees, but consult your specific shell manual/manpage to confirm. Thanks @Sorpigal.
http://gnu.org/software/bash/manual/html_node/Redirections.html
You can redirect the standard input <
standard output 1>
or >
error output 2>
or both outputs &>
but you can only redirect 1:1, you can't redirect one output into two different files.
What you are looking for is the tee
utility.
If you don't want to lose original content, you should use redirect and append >>
or <<
operators instead. You can read more here.
精彩评论