How to concatenate a file to multiple files?

If text is a file name, try:

tee -a dir/* <text >/dev/null

If text is actually some text that you want to append, then in bash:

tee -a dir/* <<<"text" >/dev/null

tee is a utility that reads from standard input and writes it to any number of files on its command line. It also copies the standard input to standard out which is why >/dev/null is used above. The -a option tells tee to append rather than overwrite.

Variation

As suggested by kvantour, it may be more clear to put the redirection for input at the beginning of the line:

<text tee -a dir/* >/dev/null

(In the above, text is assumed to be a filename)


There are problems with your code:

  • If no files in dir exists, you will write text to a file named * literally.
  • $fn expansion is unquoted!

I would:

find dir -maxdepth 1 -type f -exec sh -c 'cat text >> "$1"' _ {} \;

which I do not think is more concise.


You can do them all concisely and in parallel with GNU Parallel:

parallel 'cat text >>' ::: dir/*

Tags:

File

Bash