Friday, July 24, 2009

Find files based on modification time and copy to new directory

I've been trying to find a script to copy files from one directory to another. I stumbled upon this article but I don't want to download scripts to the server. Trying to modify it had eaten a lot of time, since I'm a bash noob.

Resolved to find a simple answer, I experimented with moving files using the find command:

find -fprintf outfile 'mv "%h/%f" targetdir"%h/%f" \n';

After generating the outfile, I chickened-out and decided to copy the files instead. One stumbling block was that plain cp by itself does not copy directories recursively.

I found the answer here:

find . -name "*.mp3" -exec cp -v --parents {} /new/directory \;

The --parents option saved the day for me.