bash, find, exec and echo

11,539

Solution 1

You have to escape the '>>', for example like this:

find . -name 'user_prefs' -exec sh -c 'echo "whitelist_from [email protected]" >> {}' \;

Solution 2

As said already, using xargs is encouraged, but you can also avoid executing sh many times by:

find . -name 'user_prefs' | while read filename; do echo "whitelist_from [email protected]" >>"$filename"; done
Share:
11,539
Roberto
Author by

Roberto

Updated on June 24, 2022

Comments

  • Roberto
    Roberto almost 2 years
    find . \
      -name 'user_prefs' \
      -exec echo "whitelist_from [email protected]" >> {} \;'
    

    I would like to add the line whitelist_from [email protected] to all files that are found by find, but my command does not work. It just creates the file '{}'.

    Shoud i use the for command?

    thanks!

  • SourceSeeker
    SourceSeeker over 13 years
    This is a correct answer, but I wouldn't exactly call it "escaping".
  • SourceSeeker
    SourceSeeker over 13 years
    This is quite a bit faster than the xargs version.