Create a bash script to remove folders that do not contain a specific file type

I recently ran into a problem.

I used the utility to move all my music files to tag based directories. This left a lot of nearly empty folders. Folders generally contain a thumbs.db file or some kind of image for album covers. Mp3s have the correct album art in their new catalogs, so old ones can be deleted.

Basically, I need to find any directories inside D: / Music / that:

-Do not have subdirectories

-Do not contain mp3 files

And then delete them.

I realized that it would be easier to do this in a shell script or bash script or in any other linux / unix world than in Windows 8.1 (HAHA).

Any suggestions? I am not a very experienced script writer.

+3
2

.

find /music -mindepth 1 -type d |
while read dt
do
  find "$dt" -mindepth 1    -type d | read && continue
  find "$dt" -iname '*.mp3' -type f | read && continue
  echo DELETE $dt
done
+3

...

find . -name '*.mp3' -o -type d -printf '%h\n' | sort | uniq > non-empty-dirs.tmp
find . -type d -print | sort | uniq > all-dirs.tmp
comm -23 all-dirs.tmp non-empty-dirs.tmp > dirs-to-be-deleted.tmp

less dirs-to-be-deleted.tmp

cat dirs-to-be-deleted.tmp | xargs rm -rf

, , , ( ), ...

...

: , , , - mp3 - , , . , .

, , , , . , ...

, , mp3 , :

find . -name '*.mp3' -printf '%h\n' | sort | uniq

" , .mp3, ".

, , , , ...

find . -type d -printf '%h\n' | sort | uniq

: " , ".

, , , . .

find . -name '*.mp3' -o -type d -printf '%h\n' | sort | uniq > non-empty-dirs.tmp

, , , .

find . -type d -print | sort | uniq > all-dirs.tmp

, , , , - , . ? , :

comm -23 all-dirs.tmp non-empty-dirs.tmp > dirs-to-be-deleted.tmp

, , , , xargs rm, .

cat dirs-to-be-deleted.tmp | xargs rm -rf
0

All Articles