The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 11:32 AM
ftp - determine ascii or binary file congo Shell Programming and Scripting 5 07-21-2007 03:53 PM
How to determine the max file size dknight UNIX for Advanced & Expert Users 2 10-27-2006 08:39 AM
determine the size of a file??? alan UNIX for Dummies Questions & Answers 8 12-31-2003 02:23 PM
How to determine if a File is Open? derrikw2 UNIX for Advanced & Expert Users 2 02-01-2002 11:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-04-2002
popo popo is offline
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.
  #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
Bits: 0 [Banking]
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
Bits: 0 [Banking]
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
popo popo is offline
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
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
  
 

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



Last edited by Kelam_Magnus; 02-04-2002 at 02:46 PM..
  #7 (permalink)  
Old 02-05-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,115
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0