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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
jail: ASCII English text
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‚‚.
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.