Linux 如何将最近的前 10 个文件从一个目录复制到另一个目录?

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

How to copy the top 10 most recent files from one directory to another?

linuxawkcopyls

提问by ThinkCode

Al my html files reside here :

我的 html 文件位于此处:

/home/thinkcode/myfiles/html/

I want to move the newest 10 files to /home/thinkcode/Test

我想将最新的 10 个文件移动到 /home/thinkcode/Test

I have this so far. Please correct me. I am looking for a one-liner!

到目前为止我有这个。请纠正我。我在找单线!

ls -lt *.htm | head -10 | awk '{print "cp "" "..\Test$1}' | sh

采纳答案by aphex

ls -lt *.htm | head -10 | awk '{print "cp "  " ../Test/"}' | sh

回答by Levon

ls -lt *.html | head -10 | awk '{print $NF}' | xargs -i cp {} DestDir

In the above example DestDiris the destination directory for the copy.

在上面的示例中DestDir是副本的目标目录。

Add -tafter xargs to see the commands as they execute. I.e., xargs -i -t cp {} DestDir.

-t在 xargs 之后添加以查看执行时的命令。即, xargs -i -t cp {} DestDir

For more information check out the xargs command.

有关更多信息,请查看xargs 命令

EDIT: As pointed out by @DennisWilliamson (and also checking the current man page) re the -ioption This option is deprecated; use -I instead..

编辑:正如@DennisWilliamson 所指出的(并检查当前手册页)重新-i选项This option is deprecated; use -I instead.

Also, both solutions presented depend on the filenames in questions don't contain any blanks or tabs.

此外,所提供的两种解决方案都取决于问题中的文件名,不包含任何空格或制表符。

回答by Barton Chittenden

Here is a version which doesn't use ls. It should be less vulnerable to strange characters in file names:

这是一个不使用ls. 它应该不太容易受到文件名中奇怪字符的影响:

find . -maxdepth 1 -type f -name '*.html' -print0 
     \| xargs -0 stat --printf "%Y\t%n\n" 
     \| sort -n 
     \| tail  -n 10 
     \| cut -f 2 
     \| xargs cp -t ../Test/

I used findfor a couple of reasons:

我使用find了几个原因:

1) if there are too many files in a directory, bash will balk at the wildcard expansion*.

1) 如果目录中的文件太多,bash 将拒绝通配符扩展*。

2) Using the -print0argument to findgets around the problem of bash expanding whitespace in a filename in to multiple tokens.

2) 使用-print0参数来find解决 bash 将文件名中的空格扩展为多个标记的问题。

* Actually, bash shares a memory buffer for its wildcard expansion and its environment variables, so it's not strictly a function of the number of file names, but rather the total length of the file names and environment variables. Too many environment variables => no wildcard expansion.

* 实际上,bash 为其通配符扩展和环境变量共享一个内存缓冲区,因此严格来说它不是文件名数量的函数,而是文件名和环境变量的总长度。环境变量太多 => 没有通配符扩展。

EDIT: Incorporated some of @glennHymanman's improvements. Kept the initial use of findto avoid the use of the wildcard expansion which might fail in a large directory.

编辑:合并了@glennHymanman 的一些改进。保持初始使用,find以避免使用通配符扩展,这可能会在大目录中失败。

回答by gil.fernandes

cpseems to understand back-ticked commands. So you could use a command like this one to copy the 10 latest files to another folder like e.g. /test:

cp似乎理解反引号命令。因此,您可以使用这样的命令将 10 个最新文件复制到另一个文件夹,例如/test

cp `ls -t *.htm | head -10` /test