Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 bash script that search in the OS ( Linux/Solaris ) the current IP address from files and replace them with other IP address.

The problem is that IP address could be in text file or in not text file as binary/data files, etc.

I use the command:

file –mime $PATH ( --mime is valid only for Linux )

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

Please advice if I do the correct thing. Is my conclusion correct?

For example if command:

file –mime $PATH

Return the results "text/plain"

Then its text/ascii file, if not then it's not text file ?

The second question:

file –mime

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

As all know file command sometimes not identify exactly the definition of the target file, so if someone have other idea how to identify text file, I will be happy to get any brilliant suggestion.

share|improve this question
text/plain doesn't tell you anything about the charset, so it doesn't have to be a ASCII file. – scai Jan 8 '13 at 7:17
so what the best solution ? how to verify if file is TEXT/ASCII ? – yael Jan 8 '13 at 7:20
you mean that I can get text/plain results even file is binary ? – yael Jan 8 '13 at 7:22
No, text/plain isn't returned for binary files of course. If you really want to know if the text uses ASCII encoding then just look at the charset as I already told you in your rather similar other question. But I suggest to learn first what ASCII actually means. – scai Jan 8 '13 at 7:43
@SCAI see Olivier Dulac solution - seems its the best verification for our quastin , what you think about ? – yael Jan 8 '13 at 8:53
up vote 1 down vote accepted
find /location -type f -print | xargs file

should be quite portable. You can then look for "[Tt]ext", etc. You'll need to do a list of the various possible outputs (script, text, ...) and see which ones you want to look into.

share|improve this answer
seems this idea much more better then file command ( can we say this syntax is the best solution ? to verify TEXT/ASCII files ) – yael Jan 8 '13 at 8:47
each tool has its use ^^ And it does use both "find" and "file" but each for their own best purpose (ie, find for finding files, and file for listing their type/purpose) – Olivier Dulac Jan 8 '13 at 8:58
OK so for the end I will use this syntax - find $PATH -type f -print | xargs file | awk '{print substr($0, index($0,$2))}' | egrep -ic "ascii|xml" ( in order to match ASCII or XML files – yael Jan 8 '13 at 9:40
@yael Are you sure you only want to match ASCII and no other charsets? – scai Jan 8 '13 at 10:13
you could maybe even drop the awk part altogether? file only outputs 1 line, and it should contain "ascii" and "xml" (and other times "text", "script", etc) so the grep would still catch it. maybe ` | egrep -ic '^[^:]*:.*ascii|^[^:]*:.*xml'` so that you don't match "xml" or "ascii" in the filename but in the "file" output ? – Olivier Dulac Jan 8 '13 at 10:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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