BASH: Adding existing filecontent to an existing file

3

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.

purplebrown

Posted 2012-04-05T09:30:20.420

Reputation:

Answers

4

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.

knittl

Posted 2012-04-05T09:30:20.420

Reputation: 3 452

1

Use xargs:

find -name 'prefs.js' | xargs -n1 bash -c 'cat content_to_be_added >> $1;' -

Fredrik Pihl

Posted 2012-04-05T09:30:20.420

Reputation: 295

1

for f in `find -name 'prefs.js'`
do
    echo $f
    #cat $f >> outfile
    cat infile >> "$f"
done

aliva

Posted 2012-04-05T09:30:20.420

Reputation: 131

nope, filecontent should be added to each prefs.js file – Fredrik Pihl – 2012-04-05T10:03:23.547

@Fredrik Fixed that. – Eroen – 2012-04-18T17:32:23.873

0

It works to write a small shell script cbd.sh

#!/bin/bash

echo "filecontent" >> "$1"

of course yo can replace echo with

cat "somefile"

and to call

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

in the same directory.

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

highsciguy

Posted 2012-04-05T09:30:20.420

Reputation: 309

1"The syntax of the find command with its -exec is a true monster." Agree! – None – 2012-04-05T10:24:51.517

0

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.

Eroen

Posted 2012-04-05T09:30:20.420

Reputation: 5 615