Skip to content

Instantly share code, notes, and snippets.

@QuietMisdreavus
Last active July 6, 2016 02:17
Show Gist options
  • Save QuietMisdreavus/8304998d19b13b0085ee9db5c2b97eb0 to your computer and use it in GitHub Desktop.
Save QuietMisdreavus/8304998d19b13b0085ee9db5c2b97eb0 to your computer and use it in GitHub Desktop.
small script to clean up my music library folders

a script to clean up my music library folders

i had a simple need: the folders of my music libary have been cluttered with old albums, that had been moved or deleted, but whose folders remained because they still had cover.jpg or AlbumArtSmall.jpg or the like left behind. i wanted these folders gone; they had no use remaining. iTunes would not see these folders (though all of these deletions/moves occurred under iTunes' watch, so you'd think it'd move the images as well *shrug*) so i took matters into my own hands.

this script searches the directory structure, starting in the current working directory. called with no arguments, it will print folders that match my definition of "empty": contain no subdirectories, nor any files that end in .mp3, .m4a, or .wav. it also prints their contents, so you can see what files have kept these folders in existence.

if you run it with the -y flag, though, it will delete the folders in question, along with the files that remained.

this script may need to be run multiple times, in case there were several "empty" folders layered within each other.

here's an example run:

$ music-clean.sh
music-clean.sh : if run with -y, this will delete directories that don't have music files in them.
(when run without, simply prints directories slated for deletion, and their contents, for verification.)
you have been warned. please use with caution. this starts in your current directory.
basing search in '/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music...'

press Enter to continue...

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/Andrew Huang/Suture Sound
Folder.jpg

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/Andrew Huang & Boyinaband/Suture Sound
Folder.jpg

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/Billy Joel/The Complete Hits Collection
AlbumArtSmall.jpg
Folder.jpg

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/Daft Punk/Homework
AlbumArtSmall.jpg
Folder.jpg

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/Masashi Hamauzu/Final Fantasy XIII Original Soundtrack
AlbumArtSmall.jpg
Folder.jpg

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/OverClocked ReMix & The Shizz/Final Fantasy_ Random Encounter
Folder.jpg

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/Unknown Artist/Unknown Album

/mnt/winhdd/Users/Bryan/Music/iTunes/iTunes Music/Music/Various Artists/The Matrix Revolutions
AlbumArt_{8E5104FB-C953-4928-9EDD-8BEA30714AE4}_Large.jpg
AlbumArt_{8E5104FB-C953-4928-9EDD-8BEA30714AE4}_Small.jpg
AlbumArtSmall.jpg
Folder.jpg
#!/usr/bin/env bash
# music-clean.sh: looks through the directories under the current
# one and removes directories that don't have music files in them.
# DANGER: "music files" is incredibly tailored to my own library.
# if you only use ogg or flac or something else that's not mp3/aac/wav,
# this script will fail in a terrible way and wipe out your library.
# please run the script with no arguments first to see what's going
# to be deleted, before committing to the destruction.
function scan_dir() {
local ifs_old=$IFS
IFS=$'\n'
#echo '>>'
local skip_me=
for sub_file in $(ls "$1") ; do
local full_file=$1/$sub_file
local lower_file=${sub_file,,*}
#echo $full_file ';' $lower_file
if [ -d "$full_file" ] ; then
#echo "directory, recurse."
skip_me=1
scan_dir "$full_file"
elif [[ "$lower_file" == *.mp3 || "$lower_file" == *.m4a || "$lower_file" == *.wav ]] ; then
#echo "music file, skip."
skip_me=1
break
fi
done
if [ -z "$skip_me" ] ; then
if [ -v full_run ] ; then
echo "rm -r $1"
rm -r "$1"
else
echo
echo $1
ls "$1"
fi
fi
#echo '<<'
IFS=$ifs_old
}
base_dir=$(pwd)
echo "$(basename "$0") : if run with -y, this will delete directories that don't have music files in them."
echo "(when run without, simply prints directories slated for deletion, and their contents, for verification.)"
echo "you have been warned. please use with caution. this starts in your current directory."
echo "basing search in '$base_dir...'"
echo
while getopts ":y" opt; do
case $opt in
y)
full_run=1
;;
*)
echo "ignoring option $OPTARG" >&2
;;
esac
done
if [ -v full_run ] ; then
echo "'delete mode' activated. this is your final warning."
fi
# in case i run in the wrong directory
echo "press Enter to continue..."
read _
scan_dir "$base_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment