A solution without piping somewhere else would be to pass the directory name to a subshell in find
's exec
function.
This will not be as efficient, and of course it'll try to remove directories that don't exist if they've already been deleted.
find . -depth -type f -name 'things.txt' -exec sh -c 'rm -rf "$(dirname $0)"' {} \;
If you want to use xargs
, you could do it very simply like so. Note that dirname
only takes one argument, so you have to specify -n 1
to have xargs
only pass one:
find . -depth -type f -name 'things.txt' | xargs -n 1 dirname | uniq | xargs rm -rf
This will not be safe if your directory name contains a newline, so be aware of that.