Skip to main content
1 of 3
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

pax

Pax is the best tool for this task. It's POSIX's replacement for cpio and tar (and unlike tar it includes a pass-through mode, not just archive creation and extraction). Sadly, it's omitted from the default installation in some Linux distributions, but it's only an apt-get/yum/emerge/… invocation away.

cd /path/to/source pax -rw -pp -T 201409080000 . /path/to/destination/ 

I'll mention other ways, that can be adapted to similar but not identical use cases where pax doesn't have a filter.

Zsh

Use the glob qualifier m to match files by their modification time (e.g. m-10 for files modified in the past 10 days; you can use other units instead), and . to match regular files. The history modifier h retains the directory part of the file name.

cd /path/to/source for x in **/*(.m-10); do mkdir -p -- $x:h cp -p -- $x /path/to/destination/$x done 

Alternatively, you can use the zmv function to copy files matched by a wildcard expression. There's no built-in way to create the destination directory, so I provide a function to do it.

autoload -U zmv mkdir_cp () { mkdir -p -- ${(P)#}:h cp -- $@ } cd /path/to/source zmv -p mkdir_cp -Q '**/*(.m-10)' '/path/to/destination/$f' 

POSIX find

With find, you can use -mtime -10 to match files modified in the past 10 days, or -newer reference_time to match files modified after reference_files.

touch -t 201409080000 /tmp/reference_time cd /path/to/source find . -type f -newer /tmp/reference_time -exec sh -c ' mkdir -p "${0%/*}" cp "$0" "/path/to/destination/$0" ' {} \; 
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k