LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Thread Tools
Old 05-31-2005, 03:47 AM   #1
Hosiah
Member
 
Registered: Sep 2004
Location: Des Moines, Iowa
Distribution: Slackware, Mandriva, Debian derivatives, +BSD/ Solaris/ Minix/ plan9/ GNU/HURD...
Posts: 185
Thanked: 0
Automatically tell text file from binary file?


[Log in to get rid of this advertisement]
OK, this has eluded me for the longest time:

I need a way to distinguish a text file. This would be something that a Bash shell script could use. I write obsessively, and I'm stuck with out a dozen fantastically useful labor-saving shell scripts I could write because I don't have a way to test if a file is text or binary without having a C program open it, read a line of bytes from it, and close it!

Surely, there must be one of the little GNU/Unix programs that can be called with the right incantation from a batch script, probably with an obscure option set, which can return a boolean yes/no on whether the file contains ASCII text characters? I know the "less" program on some systems balks at binary files and asks if you'd like to see it anyway, but that's not good enough for automated script purposes.

This is such a basic function (represented on the character level frequently in languages like Python and C as the "isascii()" function or the "isprint()" function), that I'm positive there has to be one already in existence.
Hosiah is offline     Reply With Quote
Old 05-31-2005, 03:56 AM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo
Posts: 1,127
Thanked: 3
Not sure how exactly this can be done with a script but i do know that in a color consol if you type ls -- color text files are white and binaries are green.

So ls must know somehow what the files are without opening each one.

The best way i can think of is due to binaries having the execute permission set on them. Or someway though the file system.
exvor is offline     Reply With Quote
Old 05-31-2005, 03:58 AM   #3
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 5,044
Thanked: 224
You can use "file" in your script. For details:
Code:
man file
bathory is offline     Reply With Quote
Old 05-31-2005, 06:22 AM   #4
Hosiah
Member
 
Registered: Sep 2004
Location: Des Moines, Iowa
Distribution: Slackware, Mandriva, Debian derivatives, +BSD/ Solaris/ Minix/ plan9/ GNU/HURD...
Posts: 185
Thanked: 0

Original Poster
file tests the type, I knew there was something!

Thanks!

Sometimes the most obvious answer is the only one you can't see!

Last edited by Hosiah; 05-31-2005 at 06:28 AM..
Hosiah is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
timezone binary to text file jude1980 Linux - Software 0 11-23-2005 07:07 AM
How to convert binary text file from Windows marie@dk Linux - Software 2 10-12-2005 04:17 AM
What 's the different between text file and binary file? taureanyang General 3 07-25-2005 09:37 AM
convert text file to binary excel file ust Linux - General 2 11-23-2004 03:33 AM
Determining if a file is binary or text Cyth Linux - General 3 02-27-2003 01:36 PM


All times are GMT -5. The time now is 04:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration