# Equivalent of unix "file \<filename\>" in ruby?

**URL:** <https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685>\
**Category:** Ruby\
**Created:** [October 10, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685 "2006-10-10T20:12:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 10, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685/1 "2006-10-10T20:12:38Z")

</div>

Hi all,  
I’m trying to find out what is the equivalent of file from  
unix in ruby. Basically this command gives the file type - ASCII, BINARY  
etc.

Please help!  
-Thanks

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 10, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685/2 "2006-10-10T20:50:44Z")

</div>

On Wed, 11 Oct 2006, Ruby R. wrote:

> Hi all,  
> I’m trying to find out what is the equivalent of file from  
> unix in ruby. Basically this command gives the file type - ASCII, BINARY  
> etc.

There is currently nothing written in ruby that uses a library of  
signatures to attempt to figure out the file type of a file. The best  
you  
can do right now is a library that looks at the file suffix and  
determines  
what file type matches it.

Download Austin Zeigler’s mime-types lib (when Rubyforge is playing  
nicely  
again).

mime\_type = MIME::Types.type\_for(filename)

Kirk H.

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 10, 2006, 9:10pm UTC](https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685/3 "2006-10-10T21:10:52Z")

</div>

Ruby R. wrote:

> I’m trying to find out what is the equivalent of file from  
> unix in ruby. Basically this command gives the file type - ASCII, BINARY

If your program will always run on a system where that command is  
available, you could do:  
file\_type = `file #{filename}`

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 10, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685/4 "2006-10-10T21:20:53Z")

</div>

On 10/10/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, 11 Oct 2006, Ruby R. wrote:
> 
> > Hi all,  
> > I’m trying to find out what is the equivalent of file from  
> > unix in ruby. Basically this command gives the file type - ASCII, BINARY  
> > etc.  
> > There is currently nothing written in ruby that uses a library of  
> > signatures to attempt to figure out the file type of a file. The best you  
> > can do right now is a library that looks at the file suffix and determines  
> > what file type matches it.

That’s not _quite_ true. I think it’s safer to say that there’s  
nothing currently working in Ruby that does that. However, CRUserS  
(the Calgary Ruby U.’ Society) is working on a libmagic port to  
Ruby as part of an ongoing hacking night. I’m not sure their current  
status, and hope that they’ll be setting things up to link into  
MIME::Types when it’s all done, but they are working on it and there  
is code in their source repository on RubyForge.

-austin

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 11, 2006, 2:15am UTC](https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685/5 "2006-10-11T02:15:25Z")

</div>

On 10/10/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://rubyforge.org/projects/libmagic-ruby/](http://rubyforge.org/projects/libmagic-ruby/)

Yes. Filemagic only binds to the existing libmagic, which still  
doesn’t help much for Windows users.

-austin

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 10, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/equivalent-of-unix-file-filename-in-ruby/71685/6 "2006-10-10T22:03:02Z")

</div>

On Wed, 11 Oct 2006, Austin Z. wrote:

> That’s not _quite_ true. I think it’s safer to say that there’s  
> nothing currently working in Ruby that does that. However, CRUserS  
> (the Calgary Ruby U.’ Society) is working on a libmagic port to  
> Ruby as part of an ongoing hacking night. I’m not sure their current  
> status, and hope that they’ll be setting things up to link into  
> MIME::Types when it’s all done, but they are working on it and there  
> is code in their source repository on RubyForge.

there is

[http://raa.ruby-lang.org/project/filemagic/](http://raa.ruby-lang.org/project/filemagic/)

is this their project probably?

[http://rubyforge.org/projects/libmagic-ruby/](http://rubyforge.org/projects/libmagic-ruby/)

cheers.

-a
