Unix geek question: Recursively copying only specific files?

Jump to First Reply
Posted:
in Genius Bar edited January 2014
I want to back up all of my purchased iTMS music to DVD. What I essentially want to do (even though it doesn't actually work in this form) is this:



cp -R /path/to/my/music/*.m4p /path/to/the/dvd



The problem is that the -R only works if the source parameter is a directory -- a glob expression won't do. Without -R, the glob works, but doesn't recurse.



I'm not enough of a Unix geek myself to know the magical one-line command (which I'm sure exists) that does a recursive find or something like that and pipes the find output into some other command like awk or whatever. My attempts to Google for a solution only turn up what I already know -- as if the -R of and by itself is such an awe-inspiring guru trick.

Comments

  • Reply 1 of 6
    kickahakickaha Posts: 8,760member
    find /path/to/music -name "\\*.m4p" -exec cp {} /path/to/dest \\;
     0Likes 0Dislikes 0Informatives
  • Reply 2 of 6
    shetlineshetline Posts: 4,695member
    Okay, this should get me part of the way:



    find . -name '*.m4p' -print



    That gets me the paths to all of the music files I want. I think if I played around a little more, I might get as far as copying all of these files from different directories into one flat directory.



    The Unix mojo that I lack is how to use the find output for two different things: both as-is for the source of the original file, and modified to become the proper destination path, especially in such a way as to automatically create any directories that don't originally exist within the target directory.
     0Likes 0Dislikes 0Informatives
  • Reply 3 of 6
    kickahakickaha Posts: 8,760member
    Did. I. *Stutter*?







    The above will get you the flat files.



    If you want directories recreated as well, to reproduce a hierarchy, you might want to try using rsync (grab the HFS resource fork aware one from macupdate.com, instead of the default one). It will do what you want with enough tweaking.
     0Likes 0Dislikes 0Informatives
  • Reply 4 of 6
    shetlineshetline Posts: 4,695member
    Quote:

    Originally posted by Kickaha

    find /path/to/music -name "\\*.mp4" -exec cp {} /path/to/dest \\;



    Ah... you must have posted this while I was typing my message about the piece of the puzzle I had found. Thanks.



    Wouldn't the above put all of the copied files in the same directory, instead of recreating the directory structure? I just found this recommendation for what I want to do:



    find . -name "*.pdf" -print | cpio -pvdum /newdir
     0Likes 0Dislikes 0Informatives
  • Reply 5 of 6
    shetlineshetline Posts: 4,695member
    We seemed destined to be typing some of the same things at the same time as each other.
     0Likes 0Dislikes 0Informatives
  • Reply 6 of 6
    kickahakickaha Posts: 8,760member
    *laugh* yeah, looks like.



    Will have to remember the cpio trick, thanks.
     0Likes 0Dislikes 0Informatives
Sign In or Register to comment.