Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Remove directories when specific file is in directory?
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.

Remove directories when specific file is in directory?

xrzxrz Member
edited August 2016 in Help

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

  •  find /home/test/* -type f -name '*.testfile' -printf '%h\n' | sort -u | xargs rm -rf 

    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...

    Thanked by 1xrz
  • 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.)

    Thanked by 2xrz yomero
  • xrzxrz Member

    You made my day thx both, good karm will fly to you ;)

Sign In or Register to comment.