Find the answer to your Linux question:
Results 1 to 5 of 5
Hello everybody, I'm pretty new here. I do not have a lot of skills on Linux or on scripting. So I hope that someone can help me on the following ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    3

    Copy a file to every directory (deepest level) of a directorytree

    Hello everybody,

    I'm pretty new here. I do not have a lot of skills on Linux or on scripting. So I hope that someone can help me on the following topic:

    I try to copy a generic cover.jpg file into every album directory, which does not already contains a file named "cover.jpg". The directorytree looks like as follows:

    /audio/flac/rock/Bruce_Springsteen/Darkness_On_The_Edge_Of_Town/
    /audio/flac/rock/Bruce_Springsteen/The_Ghost_Of_Tom_Joad/
    /audio/flac/rock/Huey_Lewis_And_The_News/Sports/
    /audio/flac/blues/Gary_Moore/Still_Got_The_Blues/
    .
    .
    .
    and so forth
    The most of this album directories (i.e "/audio/flac/blues/Gary_Moore/Still_Got_The_Blues/") contains a file called cover.jpg, but not every one. So I try to copy a generic cover.jpg file into these album directories. But not into the artist directory (i.e "/audio/flac/blues/Gary_Moore/") or in any other directories up in the tree. The result must be, that every album directory contains such a cover.jpg file.

    Since I have about 3000 albums in the tree, it is almost impossible to do this by hand. So, I'm looking for a shell or perl script which is able to automate this for me. Does someone has a good starting point for me?

    Many thanks

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by auti View Post
    Hello everybody,

    I'm pretty new here. I do not have a lot of skills on Linux or on scripting. So I hope that someone can help me on the following topic:

    I try to copy a generic cover.jpg file into every album directory, which does not already contains a file named "cover.jpg". The directorytree looks like as follows:



    The most of this album directories (i.e "/audio/flac/blues/Gary_Moore/Still_Got_The_Blues/") contains a file called cover.jpg, but not every one. So I try to copy a generic cover.jpg file into these album directories. But not into the artist directory (i.e "/audio/flac/blues/Gary_Moore/") or in any other directories up in the tree. The result must be, that every album directory contains such a cover.jpg file.
    Wellcome.

    Assuming that ALL the dirs are in the form artist/album, you can do so easily with sometihng like this:

    cd /my/music/; for i in */*; do if [ -d "$i" ]; then if [ ! -f "$i/cover.jpg" ]; echo cp /path/to/cover.jpg "$i"; fi; fi; done
    If what you see is what you want, then remove the "echo" and it will be done. I hope that that helps.

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    3
    i92guboj,

    first of all, many thanks for the fast reaction and the answer to my question.

    Well now I have adapted the commands to my environment and tried to get the expected result. But unfortunately I always get an unexpected return from the shell. Here the command and the error:

    bash-3.2# cd /volume1/AUDIOSTORE/RAWLib/Alternative/; for i in */*; do if [ -d "$i" ]; then if [ ! -f "$i/cover.jpg" ]; echo cp /volume1/AUDIOSTORE/cover.jpg "$i"; fi; fi; done
    bash: syntax error near unexpected token `fi'
    As you assumed, all dirs are in the form artist/album.

    I was thinking, that after the second 'if' a 'then' is missing, but this does not resolve the problem either. Any ideas what I made wrong?

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Yep, oops. Sorry. I posted that in a hurry without checking it. I forgot the second "then".

    Code:
    cd /volume1/AUDIOSTORE/RAWLib/Alternative/; for i in */*; do if [ -d "$i" ]; then if [ ! -f "$i/cover.jpg" ]; then echo cp /volume1/AUDIOSTORE/cover.jpg "$i"; fi; fi; done

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Posts
    3
    Ok I resolved the problem. It seems to be working correctly now with the following little script:
    # /bin/bash
    cd /volume1/AUDIOSTORE/RAWLib
    for i in */*/*
    do
    if [ -d "$i" ]; then
    if [ ! -f "$i/cover.jpg" ]; then
    cp /volume1/AUDIOSTORE/cover.jpg "$i"
    echo ""
    echo "cover.jpg copied to $i"
    echo ""
    else
    echo "=> cover.jpg found in $i -> no action required!"
    fi
    fi
    done
    the problem was the missing 'then' after the second 'if'

    In the previous run I haven't realized, that in the '/volume1/AUDIOSTORE/RAWLib/Alternative/' directory all artist/albums already had a cover.jpg file. So it works fine now, thanks to i92guboj

Posting Permissions

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