在shell中复制find操作的结果

时间:2018-10-25 15:10:44

标签: shell find

我想找到一个文件,并同时将其复制到另一个目录,如下所示:

cp (find . -name myFile | tail -n 1) dir/to/copy/to

但这表示意外的令牌“查找”

有更好的方法吗?

3 个答案:

答案 0 :(得分:1)

您可以使用管道:

find . -name 'myFile' -print0 | tail -z -n 1 | xargs -0 -I {} cp {} /dir/to/copy/to/

使用-print0选项使用空格,全局字符寻址文件名

答案 1 :(得分:0)

find . -name 'myFile' -print0 | tail  -n 1 | xargs -0 -I {} cp {} /dir/to/copy/to/

答案 2 :(得分:0)

有两个选项-

附加了缺少的$()-评估命令(不确定tail命令的用途,仅对于多个目录中的同一文件才需要)

  1. cp $ (查找。-name myFile | tail -n 1)dir / to / copy / to

  2. 查找。 -name myFile -type f -exec cp {} dir / to / copy / to \;