Howdy, Stranger!

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


Shells Virtual Desktop
BMail.ag - Secure Email Service
Server.net
CPLicense.net
VPS Server
Buy VPN
Vultr
VMs for AI
HostDare
HostDare
ReliableSite White-Label Dedicated Hosting for Resellers
InterServer VPS
BMail.ag - Secure Email Service
Best VPN
High-Performance Bare Metal Server Solutions
Karvl.com
Server Mania Cloud Hosting
DataWagon Hosting
AlphaVPS Hosting
Evoxt.com
Clouvider
VPS Hosting with NVMe
Residential IPs in the US & 4G Mobile Proxies in EU & US with Unlimited Bandwidth
ReliableSite White-Label Dedicated Hosting for Resellers
Rabisu - Hosting Solutions
Shells Virtual Desktop
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 Barred
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 Barred

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

Sign In or Register to comment.