Sunday, May 5, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 734  / 1 Year ago, tue, november 29, 2022, 3:26:46

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?


More From » command-line

 Answers
5

  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


[#26749] Tuesday, November 29, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
soahan

Total Points: 230
Total Questions: 123
Total Answers: 123

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
;