LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-02-2008, 12:22 AM   #1
kkmageshcse
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, 12:32 AM   #2
odcheck
Member
 
Registered: Aug 2006
Distribution: Fedora, CentOS, RHEL, Debian
Posts: 978

Rep: Reputation: 31
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
Code:
file <nameoffile>
 
Old 09-02-2008, 12:33 AM   #3
sujithri
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, 12:36 AM   #4
paulsm4
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:

http://msdn.microsoft.com/en-us/libr...50(VS.85).aspx
http://msdn.microsoft.com/en-us/libr...54(VS.85).aspx

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:
http://linuxfud.wordpress.com/2006/0...-associations/

... or ...
http://www.linuxquestions.org/questi...ation.-663861/
Quote:
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:

http://www.opengroup.org/onlinepubs/...ties/file.html
http://en.wikipedia.org/wiki/File_(Unix)
http://gnuwin32.sourceforge.net/packages/file.htm

'Hope that helps .. PSM

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

Rep: Reputation: 31
Code:
file jail.conf
= jail.conf: ASCII English text

Code:
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.

Code:
file jail
jail: ASCII English text

Code:
file bildAutobahn
bildAutobahn: JPEG image data, JFIF standard 1.02

scratch from man file

Quote:
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)
archives).

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, 12:51 AM   #6
BugZRevengE
Member
 
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:
http://unixhelp.ed.ac.uk/CGI/man-cgi?file

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

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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, 02:37 AM   #8
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

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


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:55 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