2

Taking from the last answer (by im3r3k) here: How do I recursively shred an entire directory tree?

I'm trying to get the find command to call a shell script as described.

So far I've tried:

find /home/shredtest/* -depth -exec /home/test.sh {}\;

where /home/shredtest is the directory whose contents I want to shred (but without removing /home/shredtest itself and where /home/test.sh is the script to run. I did chmod +x /home/test.sh.

The find command above returns:

find: -exec CMD must end by ';'

I've also tried:

find /home/shredtest/* -depth -exec /home/test.sh "{}"\;
find /home/shredtest/* -depth -exec /home/test.sh {}\;
find /home/shredtest/* -depth -exec sh /home/test.sh "{}"\;
find /home/shredtest/* -depth -exec sh \/home\/test.sh {}\;
find /home/shredtest/* -depth -exec sh \/home\/test.sh "{}"\;

all of which return the same error.

So:

  1. Why is the -exec failing to see the semicolon? Obviously it's there and escaped so something else must be wrong. I just don't see it.
  2. Am I even going about this the right way or is there a better way to achieve this?
JoelAZ
  • 147

1 Answers1

6

The ; has to be its own separate argument to find:

find /home/shredtest/ -depth -exec /home/test.sh "{}" \;

(note space between {} and \;). After -exec:

All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered.

(from man find). That is, the argument has to consist entirely of ; to stop the argument list.

You can also use `+' to pass many file arguments at once, which has to be its own argument too.


Note also that in find /home/shredtest/* the * is unnecessary: find will go through the directory contents itself, while * will be expanded by the shell (and occasionally may not lead to exactly the results you wanted).

Michael Homer
  • 76,565
  • Thank you Michael, will try those suggestions shortly. To address the * in find /home/shredtest/* I found, by running the find by itself, that without the trailing * the shredtest directory itself is included in the results. With the trailing * it's not. Is this not the right way or is there a better way to exclude shredtest from being shreded? – JoelAZ Jul 04 '14 at 04:54
  • You can use -mindepth 1 to process only contents and not the command-line arguments themselves. – Michael Homer Jul 04 '14 at 04:59
  • Excellent, thank you Michael. Something as silly as a space derailed me for at least 45 minutes . And mindepth seems to work a treat. Thank you! – JoelAZ Jul 04 '14 at 05:02