我正在尝试对包含许多文件夹和文件的文件夹进行搜索。我想找到最新的20个快速时间并复制到特定目录";New_Directory";。
到目前为止,我得到了这个:
find . -type f -name '*.mov' -print0 | xargs -0 ls -dtl | head -20 | xargs -I{} echo {}
这会找到我的文件并打印它们的大小/日期/名称(以./开头(
但如果我把命令改成这个(最后添加cp(:
find . -type f -name '*.mov' -print0 | xargs -0 ls -dtl | head -20 | xargs -I{} cp {} /Volume/New_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
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操作系统上使用终端。
请建议如何解决这个问题,或者建议一个更好的方法。非常感谢。
试着解构你的管道,看看发生了什么。
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
在副本中。如果你去掉长格式,你就能得到你所需要的。
顺便说明一下,如果您的find
确保了-type 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@ %pn" |
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/