Linux – Create a bash script to delete folders that do not contain specific file types

Create a bash script to delete folders that do not contain specific file types… here is a solution to the problem.

Create a bash script to delete folders that do not contain specific file types

I ran into a problem recently.

I used a utility to move all my music files to a tab-based directory. This leaves a lot of almost empty folders. Typically, these folders contain a thumbs.db file or some kind of album cover image. MP3 has the correct album art in their new catalog, so the old one can be removed.

Basically, I need to find any directory in D:/Music/

– Don’t have any subdirectories

– Does not contain any mp3 files

Then delete them.

I think this is easier to do in shell scripts or bash scripts or any other linux/unix world than in Windows 8.1 (haha).

Any suggestions? I’m not very experienced in writing scripts like this.

Solution

This should get you started

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

Related Problems and Solutions