The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to determine if a file is done copying husker_ricky UNIX for Advanced & Expert Users 2 05-22-2008 08:32 AM
ftp - determine ascii or binary file congo Shell Programming and Scripting 5 07-21-2007 12:53 PM
How to determine the max file size dknight UNIX for Advanced & Expert Users 2 10-27-2006 05:39 AM
determine the size of a file??? alan UNIX for Dummies Questions & Answers 8 12-31-2003 10:23 AM
How to determine if a File is Open? derrikw2 UNIX for Advanced & Expert Users 2 02-01-2002 07:30 AM

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2002
Registered User
 

Join Date: Feb 2002
Posts: 2
How to determine if a file is ASCII?

I'm writing a script that takes a filename as an argument, which determines the "file type" of the file. I want to know if there is any command I can use to determine if a file is ASCII type, thanks all for giving a help.
Forum Sponsor
  #2 (permalink)  
Old 02-04-2002
mib mib is offline
Registered User
 

Join Date: Jan 2001
Location: Calicut
Posts: 228
In bash it would be something like this.

if test -f "$file"
then
echo "$file: Regular File"
fi


or you can use if [ -f $file ] ....
  #3 (permalink)  
Old 02-04-2002
Jimbo
Guest
 

Posts: n/a
That test for -f will not help with an ASCII test. It determines if a filename is a regular file as opposed to a directory, a link, etc, and will return true even for binaries.

But I would start with that test, and if it is a regular file, then the "file" command will give a clue as to its content.
  #4 (permalink)  
Old 02-04-2002
witt
Guest
 

Posts: n/a
Popo,

You can try this :

for i in `find . -xdev -type f`
do
file $i | egrep -i "text|script" | awk -F\: '{ print $1 }'
done


Witt
  #5 (permalink)  
Old 02-04-2002
Registered User
 

Join Date: Feb 2002
Posts: 2
Thanks all !

I've tried something like that, but i think it does not sound good

if file "$1" | grep ascii
then
echo It is an ASCII file
elif file "$1" |grep command
...


I'm now trying all the other method you mentioned. Thanks
  #6 (permalink)  
Old 02-04-2002
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
file command?

I don't know how well it works in a script, but I have used the "file" command to tell a file type.

# file filename
filename :ascii text

# file script.sh
script.sh :commands text

It works from the command line very well.


Here is another suggestion...

Are all of your files in one directory? If so, you can do a for loop.

for name in `ls *`
do
test -f $name
some other command
some other command
done


__________________
My brain is your brain

Last edited by Kelam_Magnus; 02-04-2002 at 10:46 AM.
  #7 (permalink)  
Old 02-05-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,354
The only language that I ever have encountered with a built-in test for this was perl. I dislike perl and seldom use it. But I did give this feature a try. It seemed broken because it allowed many non-ascii characters before it finally declared a file to be binary. Since I then had to code my own test, I returned to ksh. But I do prefer perl's terminology. It calls this "text files" and "binary files".

Unless you inspect every byte of the file, you are not going to get this 100%. And there is a big performance hit with inspecting every byte. But after some experiments, I settled on an algorithm that works for me. I examine the first line and declare the file to be binary if I encounter even one non-text byte. It seems a little slack, I know, but I seem to get away with it.

Here is a little script that demonstrates this. Note that where I have used (TAB) to indicate a place where you must actually type the tab character.
Code:
#! /usr/bin/ksh
typeset -L30 fmtfile
for file in * ; do
      if read line < $file ; then
           if [[ "$line" = *[!\(TAB)\ -\~]* ]] ; then
                 type=binary
           else
                 type=text
           fi
      else
           type=unreadable
      fi 2> /dev/null
      fmtfile=$file
      echo "$fmtfile is a $type file"
done
exit 0
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0