Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous redirect when redirecting to multiple files using Bash

$ echo "" >  /home/jem/rep_0[1-3]/logs/SystemOut.log
bash: /home/jem/rep_0[1-3]/logs/SystemOut.log: ambiguous redirect

Can I redirect to multiple files at a time?

Edit: Any answer that allows use of the ambiguous file reference?

like image 287
Synesso Avatar asked Dec 22 '10 00:12

Synesso


Video Answer


2 Answers

That's what tee is for:

command | tee file1 file2 file3 > file4

tee also outputs to stdout, so you may want either to put one file after a redirect (as shown above), or send stdout to /dev/null.

For your case:

echo "" | tee /home/jem/rep_0[1-3]/logs/SystemOut.log >/dev/null
like image 177
Laurence Gonsalves Avatar answered Sep 21 '22 12:09

Laurence Gonsalves


You can do this using tee, which reads from stdin and writes to stdout and files. Since tee also outputs to stdout, I've chosen to direct it's output to /dev/null. Note that bash expansion matches against the existing files, so the files you're trying to write to must exist before executing this command for it to work.

$ echo "" | tee /home/jem/rep_0[1-3]/logs/SystemOut.log > /dev/null

As a side note, the "" you pass to echo is redundant.

Not directly relevant to your question, but if you don't rely on bash expansion you can have multiple pipes.

$ echo hello > foo > bar > baz
$ cat foo bar baz
hello
hello
hello
like image 23
moinudin Avatar answered Sep 17 '22 12:09

moinudin