This is just a question of curiosity. I've tried multiple variants, and did research, but can't seem to figure out if this is possible.
I want to execute a find command within a find command, something like the following :
find /some/dir -type d -exec find -type f -delete {} \;
So if we take the example above, this first find command finds directories in a specific folder, and the second find command deletes the files with each of those folders.
So, simply put, is it possible in bash to execute a find command within a find command? Also, why would this be a bad idea, if there is one. Could this not be used as a way to find files recursively?
In principle, you can pass a find
command as the command to execute for -exec
, just as you can any other command. It would seldom be a good idea, but that's another discussion.
However, there's a syntactic problem if both of the find
commands require -exec
. Both of the find
commands need a marker to end the command, either ;
or +
. That won't work well, even if you try mixing the end markers. The first find command will interpret the first end marker as its end marker, and it will then object to the second end marker. Since the second find
command isn't executed, it won't matter that its end marker is missing.
In your command, you have the {}
in the wrong place:
find /some/dir -type d -exec find {} -type f -delete \;
This will work (should work; I haven't tested it — I like my files!). It would make more sense if you had a condition such as -mtime +365
on the directory-level search; if a directory hasn't been modified for a year, delete the files that are in it.
However, as long as there's only one -exec
, you should be able to run find
from find
.