带有grep的xargs传递一个空字符串

时间:2016-03-10 11:19:28

标签: bash grep xargs

当我使用xargs将文件名传递给grep时,grep: : No such file or directory来自哪里?

⋊> ~/.v/bundle find . -name config | grep ".git" | xargs grep "url" {}                                                                                11:07:46
grep: : No such file or directory
./Dockerfile.vim/.git/config:   url = https://github.com/ekalinin/Dockerfile.vim.git
./nerdcommenter/.git/config:    url = https://github.com/scrooloose/nerdcommenter.git

虽然我没有空行或类似的东西

⋊> ~/.v/bundle find . -name config | grep ".git"                                                                                                      11:08:30
./Dockerfile.vim/.git/config
./nerdcommenter/.git/config

如何避免它?

我知道我可以跳过awk 'NR>1'之类的第一行,但想了解问题的原因。

谢谢。

P.S。我在Mac上。

2 个答案:

答案 0 :(得分:2)

很难说出您网站上出现了什么问题。我已经尝试重现错误,但也有.vim/bundle,但我无法重现错误。

无论如何,我建议将命令简化为:

find . -wholename '*/.git/config' -exec grep -H url {} +
由于xargs支持find语法,因此不再需要

+。对于find的GNU,BSD(MacOS)和busybox版本,这至少是正确的。

答案 1 :(得分:1)

无需添加{}(与find -exec不同)

~/.v/bundle find . -name config | grep ".git" | xargs grep "url" 

应该有用。

甚至更好(只是在包含空格或不寻常字符的文件或目录名称的情况下):

~/.v/bundle find . -name config -print0 | grep -z ".git" | xargs --null grep "url"
相关问题