Find the answer to your Linux question:
Results 1 to 6 of 6
Hi All, I'm hoping for some help sorting out my music collection please. I have a fairly large music collection on an Ubuntu 6.06 box, in a mixture of formats ...
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5

    Finding and copying certain files

    Hi All,

    I'm hoping for some help sorting out my music collection please.

    I have a fairly large music collection on an Ubuntu 6.06 box, in a mixture of formats (mostly ogg, some mp3) and the cover art is all stored as files called folder.jpg which live in the same directory as the album. The collection is laid out like this:

    /music/artist/album/tracknum trackname.extension for the music, and
    /music/artist/album/Folder.jpg for the album art.

    The thing is, my Cowon D2 will only show album art if the jpeg is called cover.jpg.

    So, I need a script to traverse the entire directory tree, looking for any file called [Ff]older.jpg, and then making a copy of each file it finds called cover.jpg. I want to keep the original folder.jpg files as other apps use them.

    I figure there must be an elegant way to do it, but I can't get it. Finding the files with a 'find' is easy, but my attempts with xargs after that have been embarrassing failures! In summary it needs to do this:

    start at top of music directory.
    Descend into all subdirectories, finding any files called folder.jpg
    Copy folder.jpg to a new file called cover.jpg in the same directory
    move to next directory.

    Any help would be greatly appreciated.

    Many thanks,

    Glenn.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    To do this with straight 'find' might be a bit tough. I might suggest a script like this:
    Code:
    #!/bin/bash
    
    IFS=$'\012'
    
    for file in $(find . -name Folder.jpg); do
        covername=$(dirname "$file")/cover.jpg
        ln -s "$file" "$covername"
    done
    This will use find to find all of the Folder.jpgs, then construct a new path with the same directory, but the file called "cover.jpg". It will then link cover.jpg to Folder.jpg (better than copying, I think).

    Hope that helps!
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5
    Quote Originally Posted by Cabhan View Post
    To do this with straight 'find' might be a bit tough. I might suggest a script like this:
    [snipped code]
    Hope that helps!
    It certainly does - spot on, thank you.

    I noticed that your script redefines the IFS variable. Is this to change the field separator so that spaces in filenames don't get munged?

    Again, many thanks for the help.

    Regards,
    Glenn.

  4. #4
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5

    Follow up

    Hi,

    I gave the suggested script a test 'as is', and found a little problem which I'm not sure I understand.

    The script creates the symlink in the same directory as the original jpg file, which is precisely what I expected and wanted. However, I don't understand the path to which the symlink points.

    For example, if I run the script from the directory /mnt/music/ABBA/ the symlink is created with a relative path, like this:

    glenn@alfie:/mnt/music/ABBA/Gold Greatest Hits$ pwd
    /mnt/music/ABBA/Gold Greatest Hits

    lrwxrwxrwx 1 glenn admin 31 2008-12-31 10:41 cover.jpg -> ./Gold Greatest Hits/Folder.jpg
    -rwxrwxr-x 1 sarah admin 6708 2007-02-15 13:29 Folder.jpg

    so the symlink points to a non existent subdirectory.

    It works fine if I run it from the actual album directory (in this case, "Gold Greatest Hits") as the symlink created then just points to "./folder.jpg".

    This being the case, should I change the script to just cd into the correct directory before creating the symlink? Unfortunately I can't easily make a copy of the collection to test with, and I don't want to create thousands of incorrect 'cover.jpg' files everywhere!

    Thanks for the help,

    Glenn.

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I noticed that your script redefines the IFS variable. Is this to change the field separator so that spaces in filenames don't get munged?
    Exactly. By default, the for loop will split on spaces, which is great if you're listing a bunch of values, but makes things very difficult when you are dealing with filenames.


    As for the relative path issue, that's my error. I forgot that find returns relative paths, not absolute paths. Try this version instead, which should turn them into absolute paths ($PWD is the current working directory) and thus fix this.

    Code:
    #!/bin/bash
    
    IFS=$'\012'
    
    for file in $(find . -name Folder.jpg); do
        covername=$(dirname "$file")/cover.jpg
        ln -s "$PWD/$file" "$PWD/$covername"
    done
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5

    Thumbs up Works a treat!

    Quote Originally Posted by Cabhan View Post
    Exactly. By default, the for loop will split on spaces, which is great if you're listing a bunch of values, but makes things very difficult when you are dealing with filenames.
    Excellent, thanks for the explanation. I'll get my head around Bash scripting yet!

    Quote Originally Posted by Cabhan View Post
    As for the relative path issue, that's my error. I forgot that find returns relative paths, not absolute paths. Try this version instead, which should turn them into absolute paths ($PWD is the current working directory) and thus fix this.
    This version does the job - nice one, and many thanks. Since your first suggestion I'd been playing with similar ideas, but didn't hit on using the $PWD variable. Thanks again - job done. My Cowon D2 now has album art!

    Regards,

    Glenn.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...