LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > 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
  Search this Thread
Old 05-31-2005, 02: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

Rep: Reputation: 31
Automatically tell text file from binary file?


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.
 
Old 05-31-2005, 02:56 AM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 05-31-2005, 02:58 AM   #3
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
You can use "file" in your script. For details:
Code:
man file
 
Old 05-31-2005, 05: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

Original Poster
Rep: Reputation: 31
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 05:28 AM.
 
  


Reply



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 06:07 AM
How to convert binary text file from Windows marie@dk Linux - Software 2 10-12-2005 03:17 AM
What 's the different between text file and binary file? taureanyang General 3 07-25-2005 08:37 AM
convert text file to binary excel file ust Linux - General 2 11-23-2004 02:33 AM
Determining if a file is binary or text Cyth Linux - General 3 02-27-2003 12:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration