Trying to grep a find -print0 causes strange behavior

James Ko

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?

Heath Malmstrom

This is expected behavior of using '-v' option of grep. It's doing what your telling it to by not giving you any lines that contain Consolas. To avoid this don't use -print0 and post process with xargs (as you've already discovered) or if you really needed to run everything together do it after the 'grep -v' filters via something like this:

echo -n $(find . -name '*.ttf' | grep -v "System Volume Information" --null | grep -v "Consolas" --null)

but you probably don't need that for this problem as xargs is sufficient.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Strange behavior of find()

Putting a Hashmap into itself causes strange behavior

Store output of find with -print0 in variable

Strange behavior with bash find command

Strange behavior of the "find" command on RH

A strange behavior for globbing in find command

In Clojure, passing number with leading 0s to str causes strange behavior. What feature is this?

strange behavior when trying to update crud

Trying to understand ::before - strange (to me) behavior?

Strange behavior trying to connect SQLite database

Trying to build trie with classes in python - strange behavior

Trying to understand strange behavior of format specifier in printf

GNU find "or" function is not working with -print0 | xargs -0 tar

Trying to understand the behavior of grep - line seperator

Strange behavior for find if pattern following -name is not quoted

Accidentally loading angularjs multiple times causes strange behavior

argc, argv causes strange behavior in Null terminator in C

VBA code for moving completed rows to another sheet causes strange behavior

Changing the value of a css variable from JS causes strange behavior

What is wrong with my use of "find -print0"?

find -print0 with 2 files only shows 1

POSIX alternative to GNU find's -print0

Got less output with print0 option on find?

Strange result when trying to print

Strange behavior while trying to get the most recent LogStream from CloudWatch

Strange behavior trying to add views programmatically to a ContraintLayout in Kotlin

Strange behavior when trying to append a row to each group in a group by object

Trying to response Amazon's Captcha with scrapy, strange behavior on spider generator

int works fine, but causes some jerky motion... Number causes strange behavior in AS3