Shell – How to copy modified files while preserving folder structure

command linefile-copyfilesshelltimestamps

I have made many changes to files in my PHP project and I want to push those changes to our server via FileZilla FTP. Rather than push all of the files, I would like to find only the files that have been modified in the last 14 days and copy those to a /ModifiedFiles folder, preserving the folder structure. This way I can simply drag the contents of the /ModifiedFiles folder into FileZilla and it will overwrite all the necessary files in the appropriate folders.

If I was using Windows, I could run this line of code and it would accomplish what I want:

xcopy RUF_Donation_Web ModifiedFiles /S /V /I /R /D:02-09-2016

How can I accomplish this in my Bash terminal on my Mac (OS X Yosemite)?

Best Answer

That's a typical job for cpio or pax:

find . -type f -mtime -14 -print0 | pax -0 -rw /ModifiedFiles

You could also use the -l option, to make links instead of copies. It doesn't work properly with the pax command on Debian, but maybe the one on OS/X doesn't have the same problem.

You can do something similar with cpio (an ancestor of pax), but the cpio implementation on OS/X doesn't seem to support a -0/--null option which would allow arbitrary file names. If you know your file names don't contain newline characters, you can still do:

find . -type f -mtime -14 | cpio -dp /ModifiedFiles

Both Debian (GNU) and OS/X versions of cpio also have a -l option to make links instead of copies. (and the one of Debian seems to work properly).

Related Question