New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Remove directories when specific file is in directory?
Hello
I need little help i have following cmd:
this only able to delete files but not folder which contain that file
find /home/test/* -name "*.testfile" -type f -exec rm -i {} \;
how to delete whole directory if file called somethingblahblah.testfile is there?
Thanks you
Comments
BE CAREFUL
I'll advise only once to do a dryrun (without the last part) at first to see what dirs get listed. and take special care which path you search in, rm -rf is forcing recursive removal on all directories found...
You could try something like
find /home/test/* -name "*.testfile" -type f -exec sh -c 'f="{}"; rm -i "$f" && rmdir "${f%/*}"' \;
buy you're playing with fire here.
(The above will attempt to remove the containing directory, but but the rmdir will fail if the directory is not empty. I presume this is what you want.)
You made my day thx both, good karm will fly to you