Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a little project to build a bash script that searches the OS (Linux/Solaris) for the current IP address in files, and replaces them with another IP address.

The problem is that IP address could be in text files or in non-text files such as binary/data files.

I use the command:

 file --mime $PATH (--mime is valid only for linux)

In order to verify if a file is a text file, or not a text file (as binary or data file etc.)

Is that correct?

For example if I have the command:

file --mime $PATH (returns the results "text/plain")

Then it's a text/ASCII file, if not then it's not a text file?

The second question:

file --mime "--mime"     

The flag is valid only for Linux but not in Solaris, so what is the alternative for Solaris?

The file command sometimes does not identify exactly the definition of the target file, so if someone has other ideas on how to identify the text file…?

share|improve this question
up vote 0 down vote accepted

No, that won't work correctly. Try e.g. running it on a HTML file.

If I remember correctly, it is common to look for a null byte (except it won't work for UTF-8 files). For plain ASCII files, this will do it:

$ hexdump -v -e '/1 "%02X "' <"$file" | fgrep -q 00
share|improve this answer
hexdump -v -e '/1 "%02X "' <"/etc/hosts" | fgrep -q 00 But this not returned anything - why hosts is text file ? – yael Jan 8 '13 at 8:29
@yael Look at the exit code ($?): 0 = null byte found; 1 = no null byte found. – Aluísio A. S. G. Jan 8 '13 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .