Find the answer to your Linux question:
Results 1 to 10 of 10
Ok so I have a directory that I want to copy all the changed mp3 files from to another. I want it to retain folders and traverse as far down ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    14

    Get all mp3 files within a directory tree

    Ok so I have a directory that I want to copy all the changed mp3 files from to another. I want it to retain folders and traverse as far down as it needs to go. Right now it only stays in the root directory. Here is my code

    Code:
    #!/bin/bash -vx
    
    /bin/cp	-u -v -p -R /media/Music/*.mp3 ~/Music/ >> ~/Scripts/music_backup.log 2>&1
    It only copied a single mp3 which happened to be in the Music/ directory, and didn't traverse into any sub folders. This is probably an easy answer for the pros, so thanks for your responses in advance.

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    2>&1

    what is this for?
    I'm not a shell expert, but it seems unnecessary

  3. #3
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    Location
    West Yorks
    Posts
    4,344
    How about using the find command along with the exec option?
    Code:
    find /media/Music -name "*.mp3" -exec cp -uvpR '{}' \;
    I've not tested the commands above, but hopefully you'll get an idea about how to solve your problem.

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Posts
    6
    Would it not be better to use something like rsync to do this task? I am new to all this so can't help you a great deal, but I think rsync is designed for this sort of task more than running scripts.

    Marc

  5. #5
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Quote Originally Posted by daark.child View Post
    How about using the find command along with the exec option?
    Code:
    find /media/Music -name "*.mp3" -exec cp -uvpR '{}' \;
    I've not tested the commands above, but hopefully you'll get an idea about how to solve your problem.
    Don't forget the destination for cp!
    Code:
    find /media/Music -iname *.mp3 -exec cp  '{}' ./ \;
    That's if you want to copy into one directory. If you want to mirror the structure use rsync
    Code:
    rsync -uav /media/Music ./Music
    Assuming you have a directory called Music in the current directory.

    Quote Originally Posted by coopstah13 View Post
    2>&1

    what is this for?
    I'm not a shell expert, but it seems unnecessary
    This redirects all standard errors and standard outputs so there is no output to the terminal. It's probably not that useful in this case for debugging.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quoth coopstah13:
    2>&1

    what is this for?
    Quoth bigtomrodney:
    This redirects all standard errors and standard outputs so there is no output to the terminal.
    Not quite. It redirects all standard error to standard output. So far in this thread standard output and standard error are sent to the screen (I would have said "sent to the teletype" in the old days), so it has no discernable effect.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Quote Originally Posted by wje_lf View Post
    Not quite...
    I stand corrected

  8. #8
    Just Joined!
    Join Date
    Nov 2007
    Posts
    14
    Code:
    rsync -uav /media/Music ./Music
    where in this code does it specify *.mp3 only?

  9. #9
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Where is my head today. Can you tell I'm in work

    Code:
    rsync -uav --include='*/*.mp3' --exclude='*' /media/Music ./Music
    So this should exclude anything that is not an mp3 or a directory containing an mp3.

  10. #10
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    Location
    West Yorks
    Posts
    4,344
    Quote Originally Posted by bigtomrodney View Post
    Don't forget the destination for cp!
    Code:
    find /media/Music -iname *.mp3 -exec cp  '{}' ./ \;
    Thanks for the correction. I must have been having one of those days when the brains just don't work as they should do.

Posting Permissions

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