代码之家  ›  专栏  ›  技术社区  ›  user3685285

在shell中复制find操作的结果

  •  0
  • user3685285  · 技术社区  · 6 年前

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

    但上面写着意外的标记“find”

    有更好的办法吗?

    3 回复  |  直到 6 年前
        1
  •  1
  •   anubhava    6 年前

    您可以使用管道:

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

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

        2
  •  0
  •   user3914897    6 年前
    find . -name 'myFile' -print0 | tail  -n 1 | xargs -0 -I {} cp {} /dir/to/copy/to/
    
        3
  •  0
  •   Jatish    6 年前

    有两种选择-

    追加缺少的$()-以计算命令(不确定tail命令的用途,仅在多个目录中的samefile需要)

    1. $ (找到。-名称myFile | tail-n 1)dir/to/copy/to

    2. 找到。-名称myFile-type f-exec cp{}dir/to/copy/to;