TransWikia.com

How do I delete all directories with a certain file type in them?

Unix & Linux Asked on December 6, 2021

I’m writing a command line script to ‘unpack’ folders with .svg files from downloads. I’ve copied the required files to a specific directory and now I need to delete all the directories with .svg files in them because they are full of other formats and licences, which I don’t need. How do I do this?

2 Answers

Using GNU find we employ find within find to first look at a dir starting from the lower levels and then check whether that dir comprises atleast one .svg file. We delete such dirs.

$ find . -depth  ! -name . -type d  
    -exec sh -c '
      N=$#
      for d do
        test "x$(find "$d/." -maxdepth 1 -type f -name "*.svg" -print -quit)" != "x" &&
          set -- "$@" "$d"
      done
      shift "$N"; rm -rf ${1+"$@"}
 ' find-sh {} +

Answered by Rakesh Sharma on December 6, 2021

GNUly, that could be:

LC_ALL=C find . -name '*.svg' -type f -printf '%h' |
  LC_ALL=C sort -zu |
  xargs -r0 echo rm -rf

Where find reports the dirname of all .svg regular files, sort -zu removes duplicates, and xargs rm removes them.

That's not terribly efficient in that it keeps looking for .svg files in children of a given directory even after .svg file have been found in it.

Note that if the current directory contains .svg files, rm will refuse to delete it (as a safeguard on rm -rf . to work around a misfeature in some shells where rm -rf .* includes . and .. in the expansion of .*).

You can work around that by replacing . with "$PWD" above.

Remove the echo when you're satisfied it will do what you want.

With the bosh shell, you could do it more efficiently with its builtin find:

has_svg() {
  find "$@" -maxdepth 1 -name '*.svg' -type f -call return 0 ;
  return 1
}

find . -type d -call 'has_svg "$1"' {} ; -prune -exec echo rm -rf {} +

Answered by Stéphane Chazelas on December 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP