0

Possible Duplicate:
bash: find -exec and filenames

I'm using a find command to find a series of files all with the same name. I then want to append the contents of a text file to the bottom of each of those files that my find command located. I thought this would be straightforward enough but I can't seem to get it to work right. Here is my command:

find . -type f -name 'my_file.php' -exec cat new_include.txt >> {} \;

When I run this using sudo, I get a permission denied error:

-bash: {}: Permission denied

If I su into the server and run it as root, I don't get an error but none of the files actually update. Am I missing something here? (This is being done on a CentOS 6 server, if that makes a difference.)

Community
  • 1
  • 1
Cloudkiller
  • 1,616
  • 3
  • 18
  • 26
  • I understand that the eventual solution is the same as the question that was marked as the duplicate to my question, but to someone unfamiliar with bash, calling this an "exact duplicate" seems a bit over the top. – Cloudkiller Oct 01 '12 at 20:34
  • Regardless, for those looking for an answer to this problem, here is the solution that the duplicate question let me to: `sudo find . -type f -name 'my_file.php' -exec bash -c 'cat new_include.txt >> "{}"' \;` – Cloudkiller Oct 01 '12 at 20:35

1 Answers1

2

I could not get this working with a single find command, but doing it sequentially worked:

for myFile in `find . -type f -name 'my_file.php' -print`
do 
    cat new_include.txt > ${myFile}
done
mana
  • 6,347
  • 6
  • 50
  • 70
  • 1
    I think that you could also do `find . -type f -name 'my_file.php' -print | xargs cat > "$myFile"` – Janito Vaqueiro Ferreira Filho Sep 28 '12 at 13:14
  • Did this work on your computer? I tried that code and ... well ... nothing. There is some syntax error in it. – mana Oct 01 '12 at 07:17
  • It works here. It would only "block" if `find` doesn't find anything. In that case, `cat` would be called without parameters, and it will assume that it was asked to read from stdin, which means it will block waiting for input. Looking at it again, I think I misread your code. A correct alternative would be: `cat new_include.txt | tee $(find . -type f -name 'my_file.php' -print | sed -e 's/ /\\ /g') > /dev/null`. I'm terribly sorry for my misunderstanding... =( – Janito Vaqueiro Ferreira Filho Oct 01 '12 at 11:15