Script to move files to new subdir

Discussion in 'Mac Programming' started by Cynthia Blue, Mar 27, 2017.

  1. Cynthia Blue macrumors newbie

    Cynthia Blue

    Oct 16, 2009
    I'm new to Applescript and Automator... I have done some limited cron scripting in my past but will need to relearn how to do it.

    What I'm looking to do is move all my .mp4 and .mov files from my hard drive to my external backup drive that's connected via my network. So basically just from one directory to another.

    I want to find all the .mp4 and .mov files that exist, and then if the corresponding subdirectory does not exist in the other directory, create it (same name) and move the files over. If it does exist, just move the files.

    What would be the easiest way to do this? I've been just copying them manually and I'm getting tired of it so want to automate it. For past directories, it would be a one-time thing. For 2017, I would need to do this every time I put my phone images and videos onto my hard drive.

  2. Alrescha macrumors 68020

    Jan 1, 2008
    cp -rp

    (~5 times faster than rsync)
    (optionally, with -n to not overwrite existing)

  3. hughm123 macrumors newbie

    Dec 3, 2014
    If you only want the .MOV and .MP4 commands then you may need a couple of other commands in a terminal.

    Step 1:
    cd /directory/to/copy/from
    find . -iname '*.mp4' -print > /tmp/copyfiles
    find . -iname '*.mov' -print >> /tmp/copyfiles

    Then for rsync:
    rsync -axH --files-from=/tmp/copyfiles . /backup/directory

    If you want a (complex) one-line command then you can use tar instead:

    ( find /directory/to/copy/from \( -iname '*.mov' -o -iname '*.mp4' \) -print0 | xargs -0 tar cf - ) | (chdir /backup/directory ; tar xfp -)

    To break that down:
    Command #1: ( find /directory/to/copy/from \( -iname '*.mov' -o -iname '*.mp4' \) -print0 | xargs -0 tar cf - )
    a. the first half is the two find command above, printing to stdout with names separated by NUL
    find /directory/to/copy/from \( -iname '*.mov' -o -iname '*.mp4' \) -print0
    b. then this is piped into xargs to make a tar file with only those files (on the overall output rather than a temp file)
    xargs -0 tar cf -

    Command #2: change directory to the target directory, then un-tar the results
    chdir /backup/directory ; tar xfp -
  4. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
  5. Cynthia Blue thread starter macrumors newbie

    Cynthia Blue

    Oct 16, 2009
    Thanks for the replies, that gives me some good information.

Share This Page