-1

So I have a music folder full of different file formats all mixed together. It IS well-structured: Music/[Artist Name]/[Album Name], with compilation albums in a folder called "Various". Some folders contain just .mp3, .m4a, or .flac files, and some have multiple versions of the album in different file formats. There are also of course various .jpegs of cover art and many of the artist folders contain a .jpeg of the artist's portrait, and there are also many miscellaneous .cue and .log files and other junk.

What I want to end up with is a separate music folder for .flac files, retaining the existing folder structure, removeing them from the existing library. I don't want to end up with empty folders in my current library either, where the album was only in flac. In cases of album folders with multiple formats, I want to move the flacs to the new library along with the cover art, but of course keep the existing cover art in place as well.

I haven't been able to find an application capable of handling this, but I figured a shell script could probably handle it. However I am pathetic with bash and really don't want to break my library.

The files are on a remote disk that I can access with mac, windows, or linux so any approach is good.

Just to fully clarify, here's the logic I'm hoping to code:

  1. Find each subdirectory of /Music that contains .flac files

  2. Copy each of these directories in their entirety (and intermediate parent directories) to a new location in /FLAC, but exclude other audio filetypes (.mp3, .m4a, etc.) from the copy.

  3. If the directory has no other audio filetypes than .flac, delete the entire directory. If it DOES have other audio files, just delete the .flac files.

  4. Do one final sweep through all of /Music to delete any directory that contains no audio files in itself or any subdirectory.

  • 4
    What have you actually tried so far? – Don Cruickshank Mar 31 '13 at 18:12
  • I've looked at a few different backup tools but I havent found one that can be quite this specific - there are many that can delete emptied folders, but since many of the flac-only folders contain other files like jpegs, it won't be an empty folder and will get left behind. – user2229787 Mar 31 '13 at 20:43

2 Answers2

1

Here's another approach:

find . -type f -name \*.flac -printf "%h\n" | 
sort -u | 
while read -r dirname; do
    new="../flac/$dirname"
    echo mkdir -p "$new"
    echo mv "$dirname"/*.flac "$new"
    jpgs=("$dirname"/*.jpg)
    [[ ${#jpgs[@]} -gt 0 ]] && echo cp "$dirname"/*.jpg "$new"
done
  • find with -printf will print out the directory name of each flac file (relative to the current directory
  • sort -u to remove the duplicates -- now you have a list of directories containing flac files
  • the while loop iterates over the directories, clones the directory hierarchy under another directory (amend to suit your needs), moves the flac files, and copies jpg files if there are any.

Remove the echo commands if you're satisfies it works for you.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • This is awesome, thank you! I just updated my original question for added clarity; this answer (mostly) takes care of steps 1 & 2, but any tips for steps 3 & 4? – user2229787 Apr 01 '13 at 00:22
  • You want to delete the directory if it has no other audio files. What if it has non-audio files? – glenn jackman Apr 01 '13 at 00:28
  • This would be the case with a directory that previously only contained .flac files and .jpegs. If it has no audio files, I'm assuming it means the files were moved, and the relevant non-audio files were copied. So the directory is not needed. It's a music library directory, so every folder should either have audio files, or subdirectories with audio files. – user2229787 Apr 01 '13 at 00:52
0

First make a new parent folder, e.g. "MusicNew" which should be equivalent to the existing 'Music' folder. Then assuming your description of sub-folders and file type that you want to copy, the following lines of code should work.

IFS='\n'
for i in `find Music -mindepth 2 -maxdepth 3 -type f -name "*flac"`;
do
    echo ${i};
    newfold=`echo ${i} | sed -e 's/Music/MusicNew/g'`;
    filename=`basename ${i}`;
    fulldirpath="${newfold:0:${#newfold} - ${#filename}}";
    echo $fulldirpath/${filename};
    mkdir -p ${fulldirpath};
    mv ${i} ${fulldirpath}/;
done
iamauser
  • 11,119
  • 5
  • 34
  • 52
  • Hey thanks for your help; however if my reading skills aren't totally wrong, this script will leave the original copies in place? What I'm going for is to split the library entirely, removing the flacs from the original location. Take a look at my comment above on my original question for my full explanation of the logic I'm going for with this operation. – user2229787 Mar 31 '13 at 20:51
  • Instead of doing a `cp ${i} ${fulldirpath}` just do `mv ${i} ${fulldirpath}` – iamauser Mar 31 '13 at 20:56
  • Will that bring the other files (like .jpg's) along too? Also, will that delete the original containing folder if it's now empty after the move? – user2229787 Mar 31 '13 at 21:17
  • It shouldn't. It should find all the ".flac" files in your Music folder and move this to the new folder name "MusicNew" that will have the same sub-folder names where the original ".flac" file was. – iamauser Mar 31 '13 at 21:28
  • Ok, because I _do_ want it to do those things. – user2229787 Mar 31 '13 at 21:36
  • You need to properly quote your variables: use `"$i"` instead of `${i}` for example – glenn jackman Mar 31 '13 at 23:12