Lee
Lee

Reputation: 31040

Command line - Remove directories if they contain a particular file type

I'd like to remove some directories if they contain .png images whilst ignoring directories that do not.

I need to use command line (I'm using MinGW).

I imagine that a solution would include rm and target a directory if it contains *.png. How can this be done?

Upvotes: 1

Views: 121

Answers (2)

Lesmana
Lesmana

Reputation: 27053

find -type f -name "*.png" -printf "%h\0" | uniq -z | xargs -0 rm -rf

Upvotes: 3

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

Something like this might work:

#!/bin/bash

shopt -s globstar
ls **/*.png | while read f; do
  dirname "$f"
done | sort -uz | xargs -0 rm -rf

Upvotes: 2

Related Questions