【问题标题】:Can I use xargs as part of a directory path?我可以使用 xargs 作为目录路径的一部分吗?
【发布时间】:2016-04-29 12:31:12
【问题描述】:

我有这个目录结构:

% ls /tmp/source_dir/
aa-aa/  bb-bb/  cc-cc/  morejunk/  somejunk/

% ls /tmp/dest_dir
aa-aa/  bb-bb/  blah/  blahblah/

对于 dest_dir 中匹配 ??-?? 的每个目录,我想从 source_dir 复制相应的文件“goodfile”。我尝试了以下方法无济于事:

% cd /tmp/dest_dir

/tmp/dest_dir% \ls -d ??-?? | xargs cp /tmp/source_dir/{}/goodfile {}
cp: cannot stat `/tmp/source_dir/{}/goodfile': No such file or directory
cp: cannot stat `{}': No such file or directory
cp: omitting directory `aa-aa'

/tmp/dest_dir% \ls -d ??-?? | xargs bash -c "cp /tmp/source_dir/{$0}/goodfile {$0}"
cp: cannot stat `/tmp/source_dir/{/bin/bash}/goodfile': No such file or directory

肯定有办法在不编写单独脚本的情况下做到这一点吗?

【问题讨论】:

    标签: bash unix xargs cp


    【解决方案1】:

    这个xargs 应该可以工作:

    cd /tmp/dest_dir
    
    \ls -d ??-?? | xargs -I{} cp /tmp/source_dir/{}/goodfile {}
    

    【讨论】:

      【解决方案2】:

      另一种解决方案,但没有 xargs

      find . -iname "??-??" -exec cp goodfile "{}"  \;
      

      【讨论】:

      • 应该从 /tmp/dest_dir 执行吗?如果是这样,您必须在某处的路径中指定 source_dir ,对吗?
      • 是的。您可以先 cd 到 /tmp/dest_dir 或者只是将 enteri 路径放在命令上,例如 find /folder/to/find -iname "??-??" -exec cp /folder/goodfile "{}" \;
      • 啊,好吧,只需要稍微调整一下:find . -姓名 ”??-??” -exec cp /tmp/source_dir/{}/goodfile {} \;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多