We manufacture a linux appliance for data centers, and all are running fedora installed from the same kickstart process. There are different hardware versions, some with IDE hard drives and some SCSI, so the filesystems may be at /dev/sdaN or /dev/hdaN.

We have a web interface into these appliances that show disk usage, which is generated using "df | grep /dev/*da". This generally works for both hardware versions, giving an output like follows:

/dev/sda2              5952284   3507816   2137228  63% /
/dev/sda5             67670876   9128796  55049152  15% /data
/dev/sda1               101086     11976     83891  13% /boot

However, for one machine, we get the following result from that command:

Binary file /dev/sda matches

It seems that its grepping files matching /dev/*da for an unknown pattern for some reason, only on this box that is seemingly identical in grep version, packages, kernel, and hardware. I switched the grep pattern to be "/dev/.da" and everything works as expected on this troublesome box, but I hate not knowing why this is happening. Anyone have any ideas? Or perhaps some other tests to try?

  • Can you try df | grep -e 'dev/*da'? – ott-- Sep 25 '12 at 14:39
  • Yeah, tried egrep with the pattern as a string and got the same result. Thanks, though. – Lance Woodson Sep 25 '12 at 14:44
  • Could you please try with -E option ? that forces to use regexp – Ben Sep 25 '12 at 14:58

Probably the machine which is returning the Binary file... message has more than one disk, probably a CD drive or something.

What is happening is that if you don't protect the pattern, it will get expanded by the shell. This means that

grep /dev/*da

...gets expanded to

grep /dev/hda /dev/sda

...which means to grep, look in the file /dev/sda and return all lines that match the text '/dev/hda'.

You need to protect the pattern, such as

grep '/dev/.da'

...so that the shell doesn't expand it.

You can confirm this on the offending machine by typing

ls /dev/*da
  • This is exactly what happened. Thanks for the heads up. – Lance Woodson Sep 25 '12 at 14:45

I think the problem is about how shell send '/dev/*da' to grep. Try

echo /dev/*da 

on both systems.

Anyway I think that correct version is /dev/.da because for grep * means 'The preceding item will be matched zero or more times.'

# echo -e '#!/bin/sh\ndf | grep /dev/*da' > df.sh
# sh -x df.sh 
+ df
+ grep /dev/hda /dev/sda
Binary file /dev/sda matches

man grep

   -a, --text
          Process a binary file as if it were text; this is equivalent to the --binary-files=text option.

          If the first few bytes of a file indicate that the file contains binary data, assume that the file
          is  of type TYPE.  By default, TYPE is binary, and grep normally outputs either a one-line message
          saying that a binary file matches, or no message if there is no match.  If TYPE is  without-match,
          grep  assumes  that a binary file does not match; this is equivalent to the -I option.  If TYPE is
          text, grep processes a binary file as if it were text;  this  is  equivalent  to  the  -a  option.
          Warning:  grep  --binary-files=text might output binary garbage, which can have nasty side effects
          if the output is a terminal and if the terminal driver interprets some of it as commands.

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.