#!/usr/bin/env bash # I frequently discover some of my music is simply awful. So I file them away in # my 1-star.m3u playlist. This script reads those songs, deletes them, and # cleans up any empty folders it leaves behind. # # Usage: cleanup-music set -e shopt -s nullglob musicdir=$(awk '/music_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf") musicdir="${musicdir/#\~/$HOME}" playlistdir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf") playlistdir="${playlistdir/#\~/$HOME}" playlistfile="${playlistdir}/1-star.m3u" # Delete designated songs IFS=$'\n' declare -a to_delete=() while read -r song; do songpath="${musicdir}/${song}" [[ -f $songpath ]] && to_delete+=("$songpath") done < "$playlistfile" if (( ${#to_delete[@]} == 0 )); then echo "Nothing to delete" exit fi printf "Deleting %s songs:\n\n%s\n\nConfirm? (y/n) " \ "${#to_delete[@]}" \ "$(IFS=$'\n'; printf "%s" "${to_delete[*]}")" read -r confirm case $confirm in y|Y) echo "Deleting..." for file in "${to_delete[@]}"; do rm -fv "$file" done ;; n|N) echo "Aborted"; exit ;; *) echo "Invalid" ;; esac :>"$playlistfile" # Remove duplicates across all playlists for playlist in ${playlistdir}/*.m3u; do awk '!seen[$0]++' "${playlist}" > "${playlist}.tmp" if [[ -f "${playlist}.tmp" ]]; then rm -f "$playlist" mv "${playlist}.tmp" "$playlist" fi done # Remove empty directories find "$HOME/music" -type d -empty -delete