How can I delete content of all directories with a certain name?
I have a directory structure with many build
directories inside. I want remove their content (all files and sub-directories), but leave the directory itself (so there will be empty build
directory).
I was trying adapt solution proposed in this question: How to remove folders with a certain name however after executing this command:
find . -name "build" -type d -exec rm -r "{}/*" \;
I got a responses (for each found match)
rm: cannot remove './path/to/found/build/*': No such file or directory
When I call directly
rm -r ./path/to/found/build/*
All files inside are removed (as expected)
To get the *
in -exec rm -r "{}/*"
expanded you would need to run a shell instead of executing rm
directly, but with this option you must be careful not to introduce a command injection vulnerability. (see https://unix.stackexchange.com/a/156010/330217)
Another option is to use -path
instead of -name
find . -path '*/build/*' -exec rm -r "{}" \; -prune
Option -prune
is to avoid descending into a directory that has been removed before.