在linux中根据修改日期复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18179643/
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-08-07 00:31:21  来源:igfitidea点击:

Copying the files based on modification date in linux

linuxbashcp

提问by Kalai

It may be a duplicate question but i could not find the solution for this i want to copy a last 3 months files from one directory to another directory but i could find only to listing the files by using the following command.

这可能是一个重复的问题,但我找不到解决方案,我想将过去 3 个月的文件从一个目录复制到另一个目录,但我只能找到使用以下命令列出文件。

find . -mtime -90 -ls

I really don't know how to copy the files by using -mtime. I'm new to linux please help me.

我真的不知道如何使用-mtime. 我是 linux 新手,请帮助我。

采纳答案by devnull

Use the -execoption for find:

使用-exec选项find

find . -mtime -90 -exec cp {} targetdir \;

-execwould copy every result returned by findto the specified directory (targetdirin the above example).

-exec会将返回的每个结果复制find到指定的目录(targetdir在上面的示例中)。

回答by Paul

I guess I would first store the list of files temporarily and use a loop.

我想我会先临时存储文件列表并使用循环。

find . -mtime -90 -ls >/tmp/copy.todo.txt

You can read the list, if it is not too big, with

您可以阅读列表,如果它不是太大,则使用

for f in `cat /tmp/copy.todo.txt`;
do echo $f;
done

Note: the quotes around cat... are backquotes, often in upper left corner of keyboard

注意:cat...周围的引号是反引号,通常在键盘的左上角

You can then replace the echo command with a copy command:

然后,您可以用复制命令替换 echo 命令:

for f in `cat /tmp/copy.todo.txt`;
do cp $f /some/directory/
done

回答by Mise

Use this command:

使用这个命令:

for i in `ls -lrt | grep "jul" | awk '{print }' `; do cp $i* /some/folder/; done

回答by FatihSarigol

One can also select the exact date and time other than going back to certain amount of days

除了回到特定天数之外,还可以选择确切的日期和时间

cp  `find . -type f -newermt '18 sep 2016 20:05:00'` FOLDER

Above copies all the files in the directory that were created after 18 september 2016 20:05:00 to the FOLDER (3 months before today :)

以上将目录中在 2016 年 9 月 18 日 20:05:00 之后创建的所有文件复制到 FOLDER(今天前 3 个月:)

Be careful with the symbol for the find command, it is NOT this one: ' it is this, a backtick: ` date selection is with this: '

小心查找命令的符号,它不是这个:'它是这个,反引号:'日期选择是这样的:'

If you have files with spaces,newlines, tabs or wildcards in their names, you can use either of the solutions from Stéphane Chazelas, first is for GNU, second is for GNU or some BSDs:

如果您的文件名称中包含空格、换行符、制表符或通配符,您可以使用 Stéphane Chazelas 的任一解决方案,第一个用于 GNU,第二个用于 GNU 或某些 BSD:

find . -type f -newermt '18 sep 2016 20:05:00' -exec cp -t FOLDER {} + 
find . -type f -newermt '18 sep 2016 20:05:00' -exec sh -c 'cp "$@" FOLDER' sh {} +

回答by Ahmed Abdelsamad

Ex: select day 09/08/2017

例如:选择日期 09/08/2017

ls -l
 -rw-rw-rw-    1    root     system          943   Aug   09   02:59  File

for j in `ls -l |awk '{ if ( == "09") print }'`
    do
        mv $j $Destination;
    done