I think there's a difference between how you think shell redirection works and how it really does.
You cannot redirect the output of the shell multiple times and expect it to be redirected to all the locations you've specified. Instead, it will only be redirected to the last location, which in your case is file2
. The answer by Chaos provides a decent explanation on how such I/O Redirection works.
What you really want to do is:
$ cat example.txt | tee file1 > file2
tee
is a program that reads from the standard input and writes to multiple file descriptors. One of them is always the standard output. So we use tee
to write the output to file1
and then redirect its stdout to file2
.
Also, based on suggestions in the comments, this is a better way to do what you're looking for:
$ tee file1 > file2 < example.txt
This approach has the advantage that it redirects the stdin
instead of trying to read over a pipe. This means that the shell now needs to spawn one less process. It also eliminates what is known as "Useless use of cat".