bash 在 Linux 中复制具有特定日期的文件夹及其文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28021063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:12:32  来源:igfitidea点击:

Copy Folders and its files with specific date in Linux

linuxbashredhat

提问by EnglandYOW

Is there a way to copy folders and its contents with specific modification date in Linux, for example I have this folder A and B it contains files with modified date 2015-01-01to 2015-01-18, is it possible to copy folder A and B containing only files modified on 2015-01-01to 2015-01-08?

有没有一种方法来复制文件夹以及它与Linux的具体修改日期的内容,比如我有这个文件夹A和B包含与修改日期的文件2015-01-012015-01-18,是有可能复制文件夹A和B只载修改过的文件上2015-01-012015-01-08

I did some research and came up with this

我做了一些研究并提出了这个

find ./ -type d -exec mkdir -p {} $target{} \;
find $source -mtime +30 -exec cp -p "{}" $target \;

but after executing the 2nd line, files will be copied to the root directory on target location, not in the same structure as the source

但是在执行第 2 行后,文件将被复制到目标位置的根目录中,与源的结构不同

for example i have this source directory to be copied on the target

例如我有这个源目录要复制到目标上

/storage/subdir1/* (modified date range - 2015-01-01 to 2015-01-18)
/storage/subdir2/* (modified date range - 2015-01-01 to 2015-01-18)
/storage/subdir3/* (modified date range - 2015-01-01 to 2015-01-18)
/storage/subdir4/* (modified date range - 2015-01-01 to 2015-01-18)

would it be possible that in the target directory (/targetdir/) all sub directories will be created automatically and it contains only files with modified date (2015-01-01 to 2015-01-08)

是否有可能在目标目录(/targetdir/)中自动创建所有子目录,并且它只包含修改日期(2015-01-01 到 2015-01-08)的文件

John

约翰

回答by Karthikeyan.R.S

It will work.

它会起作用。

find A -mtime -18 -mtime +1 -exec cp \{\} B/ \;

回答by adrin

What you need is find.

你需要的是find

As mentioned here, you can copy files using findlike this:

正如这里提到的,您可以使用find如下方式复制文件:

find /home/shantanu/processed/ -name '*2011*.xml' -exec cp {} /home/shantanu/tosend  \;

Now what you need is to specify dates instead of a pattern in your findcommand, you can do it as:

现在您需要的是在find命令中指定日期而不是模式,您可以这样做:

find /path/to/source -newermt "Jan 1 2015" -and \! -newermt "Jan 10 2015" -exec cp {} /path/to/dest  \;