how does linux detects a file type.... whether it is .mp3 or .jpeg or anything.....
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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:
= 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ââ.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.