bash - 我可以使用 xargs 作为目录路径的一部分吗?

标签 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

当然有一种方法可以做到这一点而无需编写单独的脚本吗?

最佳答案

这个 xargs 应该可以工作:

cd /tmp/dest_dir

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

关于bash - 我可以使用 xargs 作为目录路径的一部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34954640/

相关文章:

linux - 通过 mmap() 加载文件时的内存使用情况

linux sed 命令 - 在 csv 行的每一端添加字符串

xargs - 为什么 xargs 不替换第二个 {}

bash 在第二个下划线之后提取并存储

unix - 理解 inode 的概念

linux - 如何循环遍历文件以创建一系列新的 .docx 文件?

python - Python 'sys.argv' 是否有参数的最大数量限制?

linux - 为什么 bash 返回负数而不是 2^64?

linux - 在 Bash 脚本中嵌入密码

python - 有没有可以自动化跨平台系统配置的python模块?