Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

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.