We all know(or will know one day) that Ubuntu(or GNU/Linux in general) does not work with extensions of file to recognize what kind of file it is.Rather it uses Magic Numbers.
A Magic Number is a number associate with every Linux file and is generally the first few bytes of the file, which identifies the file type. It is used by the Linux command line utility file which displays the information related to a file.
So when we check a file type with the file
command, it gives appropriate file type regardless of it's extensions.
So a mp3 file gives
aditya@aditya-desktop:~/Music$ file 'music.mp3'
music.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 64 kbps, 44.1 kHz, JntStereo
aditya@aditya-desktop:~/Music$ file 'music'
music: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 64 kbps, 44.1 kHz, JntStereo
aditya@aditya-desktop:~/Music$ file 'music.txt'
music.txt: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 64 kbps, 44.1 kHz, JntStereo
aditya@aditya-desktop:~/Music$
The same expt , when tried with Nautilus, it considers the file a music file when it has .mp3 or no extension, but when it is given .txt extension, it considers it a text file.
So the main question,
- Does nautilus use extensions to recognize file?
- How can I force nautilus to ignore extensions and use magic numbers?
As you have noted, yes, Nautilus use file extension (when present) as a priority to determine the file type.
You can edit the file
/usr/share/mime/packages/freedesktop.org.xml
, search for the extension you want to ignore and delete the line. Below is an example.Supose I don't want Nautilus to associate ".txt" extension and I want it to look at the "magic number" instead.
I. Edit the file:
I will look for a line with
<glob pattern="*.txt"/>
and delete it:II. Save.
III. Run:
Done!
== Effect ==
This is how an mp3 file looks like:
This is how it looks if I change the extension to txt:
This is how it looks if I edit the file above and then change the extension:
I guess the issue is that some files like "txt" are quite hard to work out from magic, so will always need a glob.
Whereas something like mp3 has a defined magic sequence.