Author Topic: Removing synced folders that were not automatically deleted (bash)  (Read 3167 times)

theta_wave

  • Sr. Member
  • ****
  • Posts: 680
Problem: For my sync settings, I have Musicbee create folders to the destination drive or virtual device based on metadata in the tags (e.g. \Music\<genre>\<artist>\(<year>.<monthdate>) <album>\<track#>. <title>).  I also have the cover saved as "folder.jpg" in the last child directory.  The problem is when the last child directory changes due to changed metadata.  I believe Musicbee normally autodeletes empty directories, however the last child directory is not empty due to "folder.jpg" still being around; in syncing, I believe files with changes are deleted and then copied over from the library to a new child directory on the sync destination rather than moved.  Folder.jpg apparently is not deleted, thus the folder is not empty and thus remains there.  So, one is left with the following:

\Music\Foo\(2018.01.10) Album\
\Music\Foo\(2018.01.10) Album [Deluxe Edition]\

In the above, it could be that the metadata for <Album> changed to include "[Deluxe Edition]" between syncs, or there was a format change corresponding to the changed metadata.  Now, the first directory has folder.jpg and nothing else.  Musicbee will not autodelete the directory due to the presence of folder.jpg.

Solution: Below are bash commands that will list and (list and) remove directories that contain folder.jpg and nothing else.  The first command is to check and make sure that the command will remove the directories that need to be removed.  The second command is self-explanatory.  Just run either of them in the root Music directory/drive.

Code: "List the directories"
shopt -s nullglob dotglob globstar; for d in **/; do files=("$d"*); if (( ${#files[@]} == 1 )) && [[ "${files[0]##*/}" = folder.jpg ]]; then echo "$d"; fi; done

Code: "Removing folder.jpg and the directory"
shopt -s nullglob dotglob globstar; for d in **/; do files=("$d"*); if (( ${#files[@]} == 1 )) && [[ "${files[0]##*/}" = folder.jpg ]]; then rm -vr "$d"; fi; done

Credit goes to the folks at #bash @ Freenode for helping me out on finding the appropriate bash command.
Last Edit: January 13, 2018, 09:11:09 PM by theta_wave