Score:0

Copy files based on date with structure of directories

lk flag

I have commend:

find /source/ -type f -newermt '18 dec 2020' -exec cp -t /target/ {} +

This commend copy only files, without structure of dirs, but i need files with his parent dir.

Second thing is why date of files is changed when i copied thems? It's possible to don't change date of modify file when i copy thems?

Score:3
es flag

There is the --preserve=all option to cp; see man cp.

But I recommend to use rsync instead of cp; it's much more versatile, and it's easy to make it keep the timestamps, and to copy only newer files. It has some learning curve with all those options that it supports, but it's well worthwhile to dive into that.

The normal use case looks about like this:

rsync -n -av /some/where/sourcedir .

This recursively replicates sourcedir to the current directory as a subdirectory sourcedir/. It won't touch files that are already there and have the same timestamp / content. The -n option means it's just a dry run so you can see what it would do (together with -v for verbose). Once you are happy with what it would do, run it without -n:

rsync -av /some/where/sourcedir .

You can call that repeatedly; if it no longer does anything, it's well and truly finished.

You can also delete files that are no longer in the source directory tree with --delete:

rsync -av --delete /some/where/sourcedir .

If you append a trailing slash to the source path, it doesn't create a sourcedir/ subdirectory on the destination, but copies it directly into that subdirectory. Together with --delete, it creates a 1:1 copy of that tree in the current directory, also deleting everything that isn't in the source subtree:

rsync -av --delete /some/where/sourcedir/ .

Again, add -n to see what it would do. In general, I highly recommend to always use -n first to confirm that it will do what you want it to do.

There are tons of other options (like --exclude=); see man rsync.

Score:0
cn flag

You can accomplish this with find and either cp with --parents, which requires the ../target to exist, though.

(builtin cd source; find -type f \
  -newermt '18 dec 2020' -exec cp --parents -at ../target {} +)

The second option uses cpio which is more performant. Whereas the previous example uses cd to remove the starting-point before copying, we accomplish this with a -printf directive and cpio -D. And this will not require an existing target before copying.

find source/ -mindepth 1 -depth -type f \
  -newermt '18 dec 2020' -printf %P\\0 | cpio -0 -pvdm -D source/ target
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.