5

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,

  1. Does nautilus use extensions to recognize file?
  2. How can I force nautilus to ignore extensions and use magic numbers?

2 Answers 2

3
  1. As you have noted, yes, Nautilus use file extension (when present) as a priority to determine the file type.

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

sudo gedit /usr/share/mime/packages/freedesktop.org.xml  

I will look for a line with <glob pattern="*.txt"/> and delete it:
enter image description here

II. Save.

III. Run:

sudo update-mime-database /usr/share/mime  

Done!

== Effect ==

This is how an mp3 file looks like:

enter image description here

This is how it looks if I change the extension to txt:

enter image description here

This is how it looks if I edit the file above and then change the extension:

enter image description here

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .