I'm trying to use find
to echo 0
into some files, but apparently this only works with sh -c
:
find /proc/sys/net/ipv6 -name accept_ra -exec sh -c 'echo 0 > {}' \;
But using sh -c
with find -exec
makes me feel very uneasy because I suspect quoting problems. I fiddled a bit with it and apparently my suspicions were justified:
My test setup:
martin@dogmeat ~ % cd findtest martin@dogmeat ~/findtest % echo one > file\ with\ spaces martin@dogmeat ~/findtest % echo two > file\ with\ \'single\ quotes\' martin@dogmeat ~/findtest % echo three > file\ with\ \"double\ quotes\" martin@dogmeat ~/findtest % ll insgesamt 12K -rw-rw-r-- 1 martin martin 6 Sep 17 12:01 file with "double quotes" -rw-rw-r-- 1 martin martin 4 Sep 17 12:01 file with 'single quotes' -rw-rw-r-- 1 martin martin 4 Sep 17 12:01 file with spaces
Using
find -exec
withoutsh -c
seems to work without problems - no quoting necessary here:martin@dogmeat ~ % find findtest -type f -exec cat {} \; one two three
But when I'm using
sh -c
{}
seems to require some kind of quoting:martin@dogmeat ~ % LANG=C find findtest -type f -exec sh -c 'cat {}' \; cat: findtest/file: No such file or directory cat: with: No such file or directory cat: spaces: No such file or directory cat: findtest/file: No such file or directory cat: with: No such file or directory cat: single quotes: No such file or directory cat: findtest/file: No such file or directory cat: with: No such file or directory cat: double quotes: No such file or directory
Double quotes work as long as no file name contains double quotes:
martin@dogmeat ~ % LANG=C find findtest -type f -exec sh -c 'cat "{}"' \; one two cat: findtest/file with double: No such file or directory cat: quotes: No such file or directory
Single quotes work as long as no file name contains single quotes:
martin@dogmeat ~ % LANG=C find findtest -type f -exec sh -c "cat '{}'" \; one cat: findtest/file with single: No such file or directory cat: quotes: No such file or directory three
I haven't found a solution that works in all cases. Is there something I'm overlooking, or is using sh -c
in find -exec
inherently unsafe?