Reputation: 11
Ok this may sound bizarre but I have a directory /PDB/ that I want to scan for all contained directories. All of these directories contain several files and a subdirectory name /pockets/ that may or may not be empty. I want to delete every parent directory and all its contents that contains an empty /pockets/ subdirectory. So far I have this code:
cd /PDB/
for D in */
do
find -maxdepth 1 -type d -empty -exec rm -r $D +
done
This does not currently execute, giving the error find: missing argument to '-exec'
Earlier I was using {} instead of $D but that only deleted the empty subdirectory.
Upvotes: 1
Views: 54
Reputation: 295288
I wouldn't use find
here at all. Consider:
#!/usr/bin/env bash
# ^^^^ - NOT /bin/sh; using bash-only features here
shopt -s nullglob # if no matches exist, expand to an empty list
for d in /PDB/*; do # iterate over subdirectories
set -- "$d"/pockets/* # set argument list to contents of pockets/ subdirectory
(( "$#" )) && continue # if the glob matched anything, we aren't empty
rm -rf -- "${d%/pockets/}" # if we *are* empty, delete the parent directory
done
...or, if you really want to use find
:
find /PDB -type d -name pockets -empty -exec bash -c '
for arg; do
rm -rf -- "${arg%/*}"
done
' _ {} +
Upvotes: 4