我可以将xargs用作目录路径的一部分吗?

时间:2016-01-22 19:41:26

标签: bash unix xargs cp

我有这个目录结构:

% 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

当然有一种方法可以在不编写单独脚本的情况下完成此操作吗?

2 个答案:

答案 0 :(得分:3)

xargs应该有效:

cd /tmp/dest_dir

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

答案 1 :(得分:1)

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

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