0

I have a folder full of directories/files with spaces in their names, and I want to copy all of the *.ttf files to another directory except for the folders Consolas and System Volume Information. Following this post, I tried:

find ... -print0  | grep -v "System Volume Information" --null | grep -v "Consolas" --null | xargs -0 cp -t destination

For some reason, this results in a Binary file (standard input) matches message from the first grep. Using egrep to combine the filters, I attempted:

find . -name '*.ttf' -print0 | egrep -v "(System Volume Information)|(Consolas)" --null | xargs...

But in this case, egrep will print out nothing to the terminal, even though there are plenty of other folders besides System Volume Information and Consolas. When I removed all but the find part of the command, I got one large chunk of text with no newlines, because of the -print0 option. Since the whole block included Consolas, by omitting it I was omitting everything. I confirmed this when I tried doing a grep Arial on the results and the whole block was printed out.

How should I prevent this behavior in grep?

4

1 回答 1

-1

这是使用 grep 的“-v”选项的预期行为。它通过不给你任何包含 Consolas 的行来做你告诉它的事情。为避免这种情况,请不要使用 -print0 并使用 xargs 进行后期处理(正如您已经发现的那样),或者如果您确实需要一起运行所有内容,请在 'grep -v' 过滤器之后通过以下方式执行此操作:

echo -n $(find . -name '*.ttf' | grep -v "系统卷信息" --null | grep -v "Consolas" --null)

但是对于这个问题,您可能不需要它,因为 xargs 就足够了。

于 2015-06-20T23:19:24.963 回答