Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-02-2008, 01:22 AM   #1
LQ Newbie
Registered: Sep 2008
Location: India
Distribution: fedora
Posts: 4

Rep: Reputation: 0
how does linux detects a file type.... whether it is .mp3 or .jpeg or anything.....

i want to know how linux detects file types.....
for eg: if i place the cursor on mp3 file linux automatically detects the file type as mp3

even if we rename the file.mp3 to file.doc
linux detects it as .mp3 only...
so how is this done.....
how linux actually detects the file type

could anyone explain it to me coz it is needed for development purpose
Old 09-02-2008, 01:32 AM   #2
Registered: Aug 2006
Distribution: Fedora, CentOS, RHEL, Debian
Posts: 978

Rep: Reputation: 30
Every file has got a descriptor in it's header bits.
I've got a good book from Koffler that's explaining how that works.

like if you do a
file <nameoffile>
Old 09-02-2008, 01:33 AM   #3
LQ Newbie
Registered: Sep 2008
Location: currently banglore native tamilnadu(vellore)
Posts: 1

Rep: Reputation: 0
actually u need to install the any one of the software names
like xine.rpm or xine.rpm files to detect the mp3 files it is the players
Old 09-02-2008, 01:36 AM   #4
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Linux doesn't.

Neither does Windows.

This is actually a function of the shell. In the case of Windows, you need to look at "Explorer", which, in turn, relies on "Shell32.dll". Here are a couple of links:

OK - so what about Linux?

Just like Windows, the basic Linux system provides *mechanism* (the ability to do file associations), not *policy* (file associations themselves). Just like Windows, the details of exactly how file associations are done - and what you need to do to modify them - will vary from shell to shell (e.g. will be different for a Gnome desktop vs. a KDE desktop).

The good news: setting and changing file associations in Linux is usually easy. Very easy.

Here are a couple of links:

... or ...
If you are using KDE, try Control Center-KDE Components-File Associations.
Your original question: how does "Linux" identify the file, despite the file suffix. The answer is one of "policy" - many programs *dont*. If you name a file with a suffix ".mp3", many programs will simply treat it as an .mp3 - regardless of whether it is or not.

For those programs that *do* verify file suffix (or, more expensively, ignore file suffix and try to analyze the file itself), almost all of them look for some "magic number" in the file. This is exactly how the (old Unix, and current Linux/BSD) "file" program works. You can study it's source here:

'Hope that helps .. PSM

Last edited by paulsm4; 09-02-2008 at 01:57 AM.
Old 09-02-2008, 01:45 AM   #5
Registered: Aug 2006
Distribution: Fedora, CentOS, RHEL, Debian
Posts: 978

Rep: Reputation: 30
file jail.conf
= jail.conf: ASCII English text

file bildAutobahn.jpg
= bildAutobahn.jpg: JPEG image data, JFIF standard 1.02

so and now (all on the cli) without any Desktop installed on the whole system btw.

file jail
jail: ASCII English text

file bildAutobahn
bildAutobahn: JPEG image data, JFIF standard 1.02

scratch from man file

they contain text, it is text that will require translation before it
can be read. In addition, file will attempt to determine other charac‚
teristics of text-type files. If the lines of a file are terminated by
CR, CRLF, or NEL, instead of the Unix-standard LF, this will be
reported. Files that contain embedded escape sequences or overstriking
will also be identified.

Once file has determined the character set used in a text-type file, it
will attempt to determine in what language the file is written. The
language tests look for particular strings (cf names.h) that can appear
anywhere in the first few blocks of a file. For example, the keyword
.br indicates that the file is most likely a troff(1) input file, just
as the keyword struct indicates a C program. These tests are less
reliable than the previous two groups, so they are performed last. The
language test routines also test for some miscellany (such as tar(1)

Any file that cannot be identified as having been written in any of the
character sets listed above is simply said to be ‚‚data‚‚.
Old 09-02-2008, 01:51 AM   #6
Registered: Oct 2005
Location: Australia
Distribution: Slackware & Kubuntu & CentOS
Posts: 106

Rep: Reputation: 17
The man page for the file command is very detailed in how to determine the filetype - binary files like mp3, executables, video, etc tend to use "magic numbers" in the header, and text files can be determined by language files...
If you want to develop your own, try reading the file man page.. by running 'man file' or 'man file' in google to bring up pages such as:

Hope this helps... and feel free to ask for more info
Old 09-02-2008, 03:24 AM   #7
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
You can try a small experiment. Copy a file such as podcast.mp3, removing the extension. Run "file podcast".
Now run your favorite file manager, such as konqueror. It will be recognized as an mp3 file. You can double click on it to play it just like the original.
Old 09-02-2008, 03:37 AM   #8
Senior Member
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
It depends on the file browser actually.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Reading .jpeg / .gif / or any image file in C in Linux amroz Linux - Newbie 1 06-03-2008 11:33 AM
Get file icon from extension, file type, or mime-type Guitarist88 Programming 3 04-21-2008 11:58 AM
how file browser detects file type without .3 sufix? Leon_Amirreza Fedora 2 03-18-2007 11:20 PM
Webmail under Mozilla: Attachments of mime type image/jpeg do not open SkipHuffman Linux - Software 2 03-28-2005 09:12 AM
mp3 players with linux (as in the portable handheld device type) poiuytrewq Linux - General 1 12-12-2004 09:00 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:43 PM.

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