3

I have searched and searched but have not found an answer to this question.

Here is the scenario: I just ripped my entire CD collection to free lossless audio codec (FLAC) for archive. I also have some high quality lossy .m4a OR .mp3 in this archive. This is stored in DIRECTORY01/artist - album/*.flac structure. All relevant files in this directory are either .flac,.m4a or .mp3

Then I transcoded this entire directory to ogg vorbis, for actual day-to-day use and for my portable media player. This is stored in DIRECTORY02/artist - album/*.ogg structure. All relevant files are .ogg.

Now I want to verify that all is well. I used diff to compare the number of folders (and verify that they are the same. Then I counted the number of flac and ogg files (respectively) in each.

Problem: there are three extra .ogg files in the .ogg folder, somehow, somewhere. As each parent directory contains 526 subfolders, I would like to know how to automatically identify the directories that differ only in file count of specific .ogg or .flac/.m4a/.mp3 file types (ignoring any .txt, .log, .nfo, .cue, .jpg, etc).

So ideally, whatever command(s) I run, the result would be a report that ignores common subdirectories with common file counts, and helps me identify specifically any directories that have differing ".ogg or .flac/.m4a/.mp3" file counts.

Is this possible? I am also open to other suggestions/logics for verifying that all X number of .flac/.m4a/.mp3 successfully transcoded to .ogg.

  • Is there only a single layer of depth in your top level directories? No subdirectories below the 'artist-album' directory? – Arronical Apr 3 '17 at 15:17
  • I've had similar needs. See this Related Q & A – Elder Geek Apr 3 '17 at 15:29
  • @Arronical, I have tried to standardize everything as much as possible. I believe that all the respective audio files that I actually need to count are in the top level folders. That said, yes there are sub-directories, mostly having to do with extras (album art, etc). – ethan Apr 5 '17 at 18:09
2

Using a for loop with find, this may not be the fastest way to achieve your goal, but should work correctly:

for dir in DIRECTORY01/*/ ; do fcount=$(find "$dir" -maxdepth 1 -type f \( -name '*.flac' -o -name '*.m4a' -o -name '*.mp3' \) -printf . | wc -c) ; ocount=$(find "${dir/DIRECTORY01/DIRECTORY02}" -maxdepth 1 -type f -name '*.ogg' -printf . | wc -c); if [[ "$fcount" -ne "$ocount" ]]; then echo "$dir has $fcount .flac .m4a and .mp3 files but ${dir/DIRECTORY01/DIRECTORY02} has $ocount .ogg files" ; fi ; done

Or more readably

for dir in DIRECTORY01/*/ ; do
  fcount=$(find "$dir" -maxdepth 1 -type f \( -name '*.flac' -o -name '*.m4a' -o -name '*.mp3' \) -printf . | wc -c)
  ocount=$(find "${dir/DIRECTORY01/DIRECTORY02}" -maxdepth 1 -type f -name '*.ogg' -printf . | wc -c)
  if [[ "$fcount" -ne "$ocount" ]]; then
    echo "$dir has $fcount .flac .m4a and .mp3 files but ${dir/DIRECTORY01/DIRECTORY02} has $ocount .ogg files"
  fi
done

Will output something similar to:

DIRECTORY01/Nirvana - Nevermind/ has 12 .flac files but DIRECTORY02/Nirvana - Nevermind/ has 11 .ogg files

For each directory where the file counts do not match. The loop will not gracefully handle error conditions such as an 'Artist - Album' directory appearing in DIRECTORY01 but not in DIRECTORY02, or differences in spelling of the 'Album - Artist' directory.

  • It doesn't have to be fast -- just relatively automated so I can walk away and/or do something else while it gives me useful information. it will still save potentially hours of work. – ethan Apr 5 '17 at 18:20
  • I added more info about the real world use scenario, where the "flac" variable actually has to account for .m4a and .mp3 lossy formats as well (i don't have everything cleanly archived in flac, unfortunately). Running into problems adopting this script for those multiple file types. – ethan Apr 5 '17 at 23:20
  • 1
    I've edited the code to deal with the other file types. find has a slightly unusual syntax when searching multiple patterns. You can add as many different patterns as you like within the brackets but the brackets must be escaped, the -o is an 'or' operator. – Arronical Apr 6 '17 at 8:32
  • thank you! this does the trick and cuts down on a lot of manual work! Thanks for explaining the find syntax. – ethan Apr 8 '17 at 18:32
3

Since you have more .ogg than .flac, a simple approach would be to find all *.ogg names and check which ones don't have a corresponding .flac name. Something like:

find DIRECTORY02/ -type f -name '*ogg' -print0 | 
    while IFS= read -r -d '' f; do 
        flac="${f//.ogg/.flac}"; 
        flac="${flac##DIRECTORY02/}"; 
        [[ -e DIRECTORY01/"$flac" ]] || 
            printf "Missing file: %s\n" "$flac"; 
done

Here's the same thing as a commented script:

#!/bin/bash

## find all files in DIRECTORY02/ whose name ends in .ogg
find DIRECTORY02/ -type f -name '*.ogg' -print0 | 
    ## Iterate over the results of the find command, saving
    ## each file as "$f". The fancy -print0 and read -d '' stuff
    ## is needed to deal with filenames that can contain newlines.
    while IFS= read -r -d '' f; do 
        ## create the new $flac variable which is $f but with ".flac"
        ## instead of ".ogg"
        flac="${f//.ogg/.flac}"; 
        ## remove the "DIRECTORY02/" from the $flac variable. If
        ## the "$f" variable was 'DIRECTORY02/artist - album/file.ogg'
        ## it is now 'artist - album/file.flac'.
        flac="${flac##DIRECTORY02/}"; 
        ## Check whether the file exists in the same subdirectory under
        ## DIRECTORY01
        [[ -e DIRECTORY01/"$flac" ]] || 
            ## If it doesn't, print
            printf "Missing file: %s\n" "$flac"; 
done
  • I like the approach of notifying which files don't exist as .flac files, feels like it will be more helpful in the long run than just notifying which albums have different counts of the respective file types. – Arronical Apr 4 '17 at 8:23
  • I agree in theory. However, until flash media use a more sophisticated filesystem standard than FAT, this isn't possible or practical without some sophisticated heuristics, as all files get renamed from their correct, technically-accurate MusicBrainz DB names (which may contain "illegal/unsafe" characters such as colons) as they get transcoded into .ogg vorbis. So in practice, this turns up a lot of false positives. – ethan Apr 5 '17 at 18:12
  • This might work if the search used metadata instead of filenames, though!? – ethan Apr 5 '17 at 18:14
  • I honestly feel like both of these responses address the question. @Arronical, your response addresses the question most directly, whereas terdon goes a bit above and beyond into the spirit of the question. – ethan Apr 5 '17 at 22:05
  • I added more info about the real world use scenario, where the "flac" variable actually has to account for .m4a and .mp3 lossy formats as well (i don't have everything cleanly archived in flac, unfortunately). Running into problems adopting this script for those multiple file types. – ethan Apr 5 '17 at 23:20
0

You might try using meld. The description is:

Meld is a graphical diff viewer and merge application for the GNOME desktop. It supports 2 and 3-file diffs, recursive directory diffs, diffing of directories under version control (Bazaar, Codeville, CVS, Darcs, Fossil SCM, Git, Mercurial, Monotone, Subversion), as well as the ability to manually and automatically merge file differences.

1) The web site is:

http://meldmerge.org/

2) And if it is in your list of repositories, you can install it with:

sudo apt-get update
sudo apt-get install meld

3) It's also in the Ubuntu Software app.

  • i had already tried meld. I should have mentioned that in the OP, sorry! while it's a great graphical diff/merge tool, it doesn't seem to have the capability to give useful information in this particular circumstance. – ethan Apr 5 '17 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.