How to determine if a file is ASCII?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to determine if a file is ASCII?
# 1  
Old 02-04-2002
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  
Old 02-04-2002
In bash it would be something like this.

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

or you can use if [ -f $file ] ....
# 3  
Old 02-04-2002
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  
Old 02-04-2002

You can try this :

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

# 5  
Old 02-04-2002
Thanks all !

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

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

I'm now trying all the other method you mentioned. Thanks
# 6  
Old 02-04-2002
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 :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 *`
test -f $name
some other command
some other command

Last edited by Kelam_Magnus; 02-04-2002 at 02:46 PM..
# 7  
Old 02-05-2002
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.
#! /usr/bin/ksh
typeset -L30 fmtfile
for file in * ; do
      if read line < $file ; then
           if [[ "$line" = *[!\(TAB)\ -\~]* ]] ; then
      fi 2> /dev/null
      echo "$fmtfile is a $type file"
exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

2. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

3. Shell Programming and Scripting

How to determine if there's a file in directory!

Hi All, I'm just wondering how can i determined if there's a file in directory and put it in a logs? dir="/home/test/" Please advise, Thanks, Use code tags, thanks. (1 Reply)
Discussion started by: nikki1200
1 Replies

4. Shell Programming and Scripting

Determine BL and RL of a file

A regular ebcdic mainframe tape usually contains header information the 1st three blocks of the tape. The header information tells the computer/user more information about what is on tape. The header info is 240 bytes in length at 80 bytes each header. The 1st block/header is volume name or... (1 Reply)
Discussion started by: Linux-wannabe
1 Replies

5. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

6. UNIX for Advanced & Expert Users

How to determine if a file is done copying

I have a file repository in a directory where files are copied into it by ftp or samba. Some of the ftp transfers can be slow, and some of the files can be fairly large. The files are not being used for anything in this directory other than being taken out of the directory and used by the... (2 Replies)
Discussion started by: husker_ricky
2 Replies

7. Shell Programming and Scripting

determine owner of a file

Hello, I am on a mission to determine the user of file. I have used the ls -l command but it displays permission, link, user, group, etc, but I just want to display just the name of user of a specified file. Many thanks (4 Replies)
Discussion started by: unibboy
4 Replies

8. Shell Programming and Scripting

ftp - determine ascii or binary file

Hello, How to i determine via ftp commandline if files on ftp server is ascii or binary files. Like every other comon windows ftp program does it automatically. regards Thomas (5 Replies)
Discussion started by: congo
5 Replies

9. UNIX for Dummies Questions & Answers

determine the size of a file???

Hello, Can someone please tell me which command to use to determine the size of a file? When I log in to my shell account, I do this $>% ls -als total 632 8 -rw-r--r-- 1 user01 devgrp1 1558 Jul 30 23:25 .kshrc What is "1158"? Bytes? Kilobytes? I apologize if my... (8 Replies)
Discussion started by: alan
8 Replies

10. UNIX for Advanced & Expert Users

How to determine if a File is Open?

I need to know what the best way, if possible in a perl or shell script, to determine if a file is open by a process, and if it is open for writing. While I would rather use a perl or shell script, if I have to use C, that would be ok. Thanks. (2 Replies)
Discussion started by: derrikw2
2 Replies
Login or Register to Ask a Question