0

I did have a read of the post at How do I execute multiple commands when using find?. Unless I have misunderstood the answer provided, I do not believe it provides the solution I am seeking.

My requirement is to find a specific set of files. If found to then create a directory. If the directory has been successfully created only then to extract the files. Once extracted to delete the source files. How would I be able to achieve this?

1 Answers1

3

The link you post gives the "correct" answer, in my opinion. Just spawn a shell. Without doing this, you'll not have a large enough toolbox. You could construct a find command in pseudocode such as:

find /path -criteria -exec mkdir {} \; -exec extract {} \; -exec rm {} \;

This will stop on any error, as predicates are naturally and'ed together. But, it gives you insufficient control over the naming of the directory. It's guaranteed to fail.

Spawning a shell resolves this as you'll have the whole shell language at your grasp:

find /path -criteria -exec /bin/sh '
    for d; do
        mkdir "${d##*/}" && tar xvf "$d" && rm "$d"
    done' _ {} +

Note that this version has the side effect of continuing on an error for any given iteration of the loop -- it spawns a single shell for all results. I'd suggest spawning the shell with the -e flag if you want to stop hard on any error.