我有这样的目录结构:
% 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
当然有一种方法可以做到这一点而无需编写单独的脚本吗?
最佳答案
这个 xargs
应该可以工作:
cd /tmp/dest_dir
\ls -d ??-?? | xargs -I{} cp /tmp/source_dir/{}/goodfile {}
关于bash - 我可以使用 xargs 作为目录路径的一部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34954640/