Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've to add the content of a file to the end of every prefs.js file. Tried it with find -name 'prefs.js' -exec more filecontent >> '{}' \; but it didn't work.

share|improve this question

migrated from Apr 5 '12 at 11:37

This question came from our site for professional and enthusiast programmers.

up vote 4 down vote accepted

Redirection does not happen for each file (this is tricky). The workaround is to spawn a new shell for each file:

find -name 'prefs.js' -exec sh -c 'cat filecontent >> $1;' - '{}' \;

The - is necessary, as it becomes the zeroth ($0) argument to sh

Besides, you have to use cat instead of more. More is a pager which allows users to scroll through a document.

share|improve this answer

Use xargs:

find -name 'prefs.js' | xargs -n1 bash -c 'cat content_to_be_added >> $1;' -
share|improve this answer
for f in `find -name 'prefs.js'`
    echo $f
    #cat $f >> outfile
    cat infile >> "$f"
share|improve this answer
nope, filecontent should be added to each prefs.js file – Fredrik Pihl Apr 5 '12 at 10:03
@Fredrik Fixed that. – Eroen Apr 18 '12 at 17:32

It works to write a small shell script


echo "filecontent" >> "$1"

of course yo can replace echo with

cat "somefile"

and to call

find -name 'cbd[0-9].txt' -exec ./ '{}' \;

in the same directory.

The syntax of the find command with its -exec is a true monster.

share|improve this answer
"The syntax of the find command with its -exec is a true monster." Agree! – purplebrown Apr 5 '12 at 10:24

How about

find -name 'prefs.js' -exec dd if=filecontent conv=notrunc oflag=append of='{}' \;

That is assuming filecontent is a file containing what you want to append to the prefs.js files.

share|improve this answer

You must log in to answer this question.