Egrep 正在根据文件的内容为我生成一个文件列表,但是我在尝试将该列表作为参数传递给 cp 时做错了。(我的外壳是 bash)。我以为我会转义文件名中的空格并将换行符转换为空格,但 cp 似乎忽略了管道输入中的转义空格。
示例文件名:2011-05-15\ 14.43.41.txt
$ cp `egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' '` ~/crashreportstemp/
cp: cannot stat `2011-05-15': No such file or directory
当我执行反引号的内容时,我得到了可以直接粘贴到 cp 中的输出。
我也尝试过使用 xargs :
$ egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' ' | xargs cp ~/crashreportstemp
但这会导致 cp 将最后传递的文件名视为最终的 cp 参数,而忽略我对 cp 的显式参数:
cp: target `2011-05-30 16.23.30.txt' is not a directory
我显然忽略了正确的方法,请帮助!
谢谢 - 杰森