Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中,查找、排序和复制_Bash_Sorting_Terminal_Copy_Find - Fatal编程技术网

在bash中,查找、排序和复制

在bash中,查找、排序和复制,bash,sorting,terminal,copy,find,Bash,Sorting,Terminal,Copy,Find,我正在尝试对包含许多文件夹和文件的文件夹进行搜索。我想找到最新的20个quicktimes并复制到特定的目录“New_directory” 到目前为止,我得到了这个: find . -type f -name '*.mov' -print0 | xargs -0 ls -dtl | head -20 | xargs -I{} echo {} 这将查找我的文件并以大小/日期/名称(以./)打印它们 但如果我将命令更改为此(在末尾添加cp): 我得到一个错误: cp: illegal optio

我正在尝试对包含许多文件夹和文件的文件夹进行搜索。我想找到最新的20个quicktimes并复制到特定的目录“New_directory”

到目前为止,我得到了这个:

find .  -type f -name '*.mov' -print0 | xargs -0 ls -dtl | head -20 | xargs -I{} echo {}
这将查找我的文件并以大小/日期/名称(以./)打印它们

但如果我将命令更改为此(在末尾添加cp):

我得到一个错误:

cp: illegal option -- w
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
cp: illegal option -- w
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory
.... (20 times)
我正在mac OS上使用终端

请建议如何解决这一问题,或建议更好的方法。
谢谢。

尝试解构您的管道,看看发生了什么

find .  -type f -name '*.mov' -print0 | xargs -0 ls -dtl | head -20 | 
提供20个最新
mov
文件的列表。《迷失》看起来像:

-rw-r--r-- 1 ljm users 12449464 Jan 10 16:24 ./05ED-E769/DCIM/215___01/IMG_5902.mov
-rw-r--r-- 1 ljm users 14153909 Jan 10 16:00 ./05ED-E769/DCIM/215___01/IMG_5901.mov
-rw-r--r-- 1 ljm users 13819624 Jan 10 15:58 ./05ED-E769/DCIM/215___01/IMG_5900.mov
因此,您的
xargs | cp
会将其作为输入

会的

cp -rw-r--r-- 1 ljm users 13819624 Jan 10 15:58 ./05ED-E769/DCIM/215___01/IMG_5900.mov /Volume/New_Directory/
如果我们看到您的错误消息

cp: illegal option -- w
cp-r
正常,
cp-rw
将生成此消息。这和我说的是一致的

所以,问题是为什么拷贝中的
-l
。如果删除长格式,就可以得到所需的内容

作为旁注,如果您的
查找
确保了
-类型f
,为什么
ls-d

find .  -type f -name '*.mov' -print0 | xargs -0 ls -t | head -20 | xargs -I{} cp {} /Volume/New_Directory/
应该做您想做的事情,但是请记住您正在解析
ls
的输出,这被认为不是一个好主意

就我个人而言,我会的

find . -type f -printf "%T@ %p\n" |
    sort -n |
    cut -d' ' -f 2- |
    tail -n 20 |
    xargs -I{} cp {} /Volume/New_Directory/
您可以使用下面的脚本

find . -type f -name '*.mov' | ls -1t | head -n 20 |
xargs -n 1 -I {} realpath {} |
xargs -n 1 -I {} cp {} /Volume/New_Directory/

尝试将cp{}更改为cp{}@RamanSailopal没有帮助,相同的错误…:(如果你把文件名传给
ls-dtl
你会得到更多的信息,比如你描述的大小/日期等。把那些字符串传给
cp
是行不通的。为什么
xargs-0 ls-dtl
部分?@0stone0谢谢你的评论,我需要按日期顺序排序,搜索最新的20个,也许我可以保留ls-dt,看起来很好。)rking..bash
find
内置了所有这些需求。我确实按照建议删除了“-l”,效果很好。“…解析ls的输出,这被认为不是一个好主意。”-为什么不呢?有什么更好的解决方案吗?谢谢!!!当我在我的google bubble中搜索“parse ls output”时,我得到的第一个结果是。作为一名TL;DR:无法跨unices移植,无法意外处理特殊字符等等。
find . -type f -name '*.mov' | ls -1t | head -n 20 |
xargs -n 1 -I {} realpath {} |
xargs -n 1 -I {} cp {} /Volume/New_Directory/