I love BASH but am struggling with GREP.

My goal is simple: GREP my Desktop files for “0.jpg”.


$ pwd
$ grep "0" *.jpg




Binary file IMG_2125.jpg matches
Binary file the-letter-just-j-bw-vectorized-v2.jpg matches


Mac OS X Yosemite


Why is GREP “0” *.jpg showing BINARY FILES rather than a SINGLE FILE named “0.jpg”?


I’m assuming the reason these files are showing up is due to the fact that they are both “Binary.” “Binary File” = the file is coded in 0’s & 1’s. Thus, both files showing up must translate as 0’s, while all the other files in my PWD translate as 1’s.


Why isn’t 0.jpg the output?


I’m assuming both the output files, in binary terms, are 0’s and all other files in the PWD are 1’s. Is this assumption correct?


Why are the resulting files 0’s? I didn’t code these files in any special way on purpose. Are the files that didn’t show up from my GREP search coded as 1’s? If so, why?


Obviously there is a lot of overlap in the functionality of each of these utilities.

What’s your favorite combination, in terms of usage?

  • It is unclear what it is you actually want to do. You say that your expected output is 0.jpg, i.e. a filename, but grep looks inside files for lines that match patterns. Could you please clarify whether you are looking for filenames or if you are trying to match the content of files? – Kusalananda Sep 1 '18 at 6:32
  • From grep manual: "grep searches for PATTERN in each FILE.". Please reconsider your question and avoid introducing discussions next time. – ploth Sep 1 '18 at 6:39
  • grep is writing out that a 0 was found in a binary file. its a warning... supposed to be a "feature." maybe it is ... the grep you use on a system containing binary files is not the grep originally designed to work on a system devoid of binary files... – mikeserv Sep 1 '18 at 6:42
  • @Kusalananda You just answered my question. I'm missing the option to limit searches to titles only Thanks for the rapid expert advice! – jlusche Sep 1 '18 at 6:47
  • @ploth I apologize for bad form. I'm new to posting to this forum, & I thought discussions were somewhat encouraged. Where would you recommend posting discussions? Thanks! – jlusche Sep 1 '18 at 6:52

The grep utility looks inside files for lines matching regular expressions.

What you (seem to) want to do is to list all files in the current directory that contain a 0 (zero) in their filenames and that has a filename suffix of .jpg. To do this, you would use something like

ls -l ./*0*.jpg

or indeed just

echo ./*0*.jpg

The first part of the pattern, *0*, would match any string that contains a zero, while the last part of the pattern, .jpg, will limit the resulting filenames to those that end with that suffix. The ./ means "in this directory" and is only really needed if you have filenames beginning with - (dash) (these would potentially otherwise be misinterpreted as command line options)¹.

The expansion of this pattern is done by the shell before calling ls, so ls would get the expanded list of filenames that match the pattern and would list these.

Note that filename globbing patterns are not regular expressions (only similar to them). grep, on the other hand, takes one or several regular expressions and searches for them in the contents of files.

Answering a question found in comments: A "warning" is a form of non-fatal diagnostic output. The other type is an "error", which is usually fatal (i.e. the program is unable to continue as usual and terminates after outputting the error message).

Diagnostic messages are usually not considered part of the standard output that a command produces during normal operation and is usually produced on a special "output stream" (on the so called "standard error stream" as opposed to on the "standard output stream") so that it may be inspected separately, or discarded, and so that it does not interfere with the ordinary output of the command.

In this case though, grep is unable to actually show the data that matched the pattern since the file is binary and the data very likely is unprintable, so instead it notifies you that there was a match in the file through the message Binary file IMG_2125.jpg matches instead (this is not actually a diagnostic warning message as mikeserv suggests).

¹ Note that if some filenames contain backslash characters, some echo implementations would interpret the \n, \b... as escape sequences to expand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.