Monday, January 26, 2015

unixish recursive copying of files newer than X

This probably isn't the most efficient way of doing this (more of a note to my future self, because this is good enough.)

Googling for how to just copy files newer than a certain date, I realized I didn't want to copy per se, that it was probably better go ahead and do the tar command:


tar -cvf newerstuff.tar --newer-mtime '22 days ago' .

Now in practice, I brought this down to my local machines to test and inspect the results. (After gzip'ing it). This command makes folders for the whole structure, even the empty folders that had nothing new in them... so  

find . -type d -empty -delete

got rid of those, and I felt a little better about the result of tarring and gzip'ing that instead. I presume there might be some way to have the first tar command ignore 'empty' folders, or the fact is it wouldn't have hurt anything if they were included when I untar'd in the new location, but still.

Update: this page shows an alternate method for skipping those directories, but it's still a little clunky, relying on a temporary file touched to reflect a certain date. 

2 comments:

  1. Are you copying from one device to another (as in, backup?) or from one machine to another? Using tar for this process is an unnecessary step unless the file archive is being created for backup purposes.

    ReplyDelete
  2. Personally, I'd use a variant on this;

    find . -depth -print | cpio -p -dumv new-path

    with whatever switches to 'find' are useful... like -type f and -ctime 3 to indicate files only, changed within the last 3 days (note: rounding errors may be encountered)

    ReplyDelete