Terminal - Delete all folders that do not contain .mp3 files

I use Banshee on Linux and I automatically organize my music collection in folder hierarchies. When I add a new album to my Music folder, Banshee automatically moves (and renames) mp3s and puts them in the correct Artist folder. If there is no other file in the folder, the folder will also be deleted, but if other files are present, only mp3 files will be moved and the folder will remain. Because of this, I have several folders inside my music folder that do not have mp3 files, only an image file or a similar auxiliary file.

How can I delete any folder (inside the "Music" folder) in which there is no mp3 file inside?

For example, suppose I have the following:

/home/user/Music/ 

and I add the folder "Album 1 (2010)", which has mp3, as well as the cover. Banshee will pull out the mp3 and put them in the appropriate folder of the artist, say:

 /home/user/Music/Artist 

but then the folder

 /home/user/Music/Album 1 (2010) 

still exists. How can I check if there is mp3 in this folder, and if not, delete it?

I believe the answer will be a command line, but I am open to any suggestion. In addition, it may be useful to require confirmation ... just in case.

+4
source share
4 answers

Based on ghostdog74 answer:

 #! /bin/bash find -depth -type d | while read -r D do v=$(find "$D" -iname '*.mp3') case "$v" in "" ) echo "$D no mp3" # rm -fr "$D" #uncomment to use ;; esac done 

Check it in the directory structure

 . ./deleteme ./save2 ./save2/x.MP3 ./save-recursive ./save-recursive/nested ./save-recursive/nested/x.mp3 ./save ./save/x.mp3 

Output:

 ./deleteme no mp3 
+2
source

First, delete a non-mp3 file in an empty folder with jpg files.

to find! -iname '* .mp3' -type f -delete

if you do not delete the empty folder

 find -depth -type d -empty -exec rmdir {} \; 
+2
source

I can’t comment, but the @ ghostdog74 find command checks the current directory for mp3 files in addition to the actual subdirectories, if there are no mp3 files in / home / user / Music, this will delete the entire tree.

If these are completely empty directories, then rmdir * will do the trick without any scripts: rmdir cannot delete files or directories with files in them.

To work with spaces, run IFS=$(echo -en "\b\n") either at the top of the command or in the shell. This prevents variables from expanding to multiple arguments when the variable contains a space. If you do this in a shell, you probably want to do something like SAVEIFS=$IFS; IFS=...; do stuff; IFS=$SAVEIFS SAVEIFS=$IFS; IFS=...; do stuff; IFS=$SAVEIFS SAVEIFS=$IFS; IFS=...; do stuff; IFS=$SAVEIFS to return the original parameter or just close the terminal and open a new one to get a new environment.

+1
source
 #! /bin/bash shopt -s nullglob shopt -s nocaseglob find -depth -type d | { while read -r D; do case "$D" in "$DR" ) continue;; esac v=$(echo "$D"/*.mp3) case "$v" in "" ) echo "$D no mp3, to be deleted";; # rm -fr "$D" #uncomment to use *) DR=${D%/*} ;; esac done } 
0
source

All Articles