Different file formats have different ‘characteristics’ embedded into them, so the operating system can use that information to distinguish them and assign different icons, associate programs (for opening) etc, so at the end, users can deal different files with ease.
For example, let’s say due to a virus, all of a sudden the icons and the ‘extensions’ were gone from your OS. Then the text, video, audio, saved web pages, PDF, word file icons etc will all look the same and the file association should also be gone. And I don’t have to tell you the hassle one will have to go through after that :).
Though changing the ‘extension’ in both GNU/Linux and Windows can ‘confuse’ the OS and break the program association (if you changed the ‘.txt’ extension of a text file into ‘.mp3′, then the OS will open the text file with the audio player :D), however, if you delete the extension, then Windows will ask you to manually chose a program. But, GNU/Linux will still be able to detect its ‘type’ and open it using the assigned program.
But what if you have a file with a wrong extension in GNU/Linux, and the operating system will open it using the wrong programs. And sometimes, even if it has the correct extension (assuming that you don’t know much about it), if there isn’t a program installed for opening it, the OS might not be able to detect its type/format and give you at least some information which you can use to find a program for opening it.
So unless you wanna try opening it using all of your installed apps and many others to come (yikes!), you can use this small but a very useful tool that comes with GNU/Linux which can identify a huge number of file types, even if they have the wrong (or unknown) extension or don’t have one at all.
It’s called ‘file’ and it tries to identify a file and its actual type by running three tests (file system test, ‘magic number’ test and language test) and specially the ‘magic number’ test, which looks for a certain ‘code’ that is unique to each file type and then tries to determine the actual format by using it and comparing it with its database for ‘magic numbers’, usually does the trick.
For instance, the ‘.flv’ (flash video) extension has the unique code of ’46 4C 56 01′ code, PDF has the code ’25 50 44 46′ etc, and this ‘code’ is embedded at the very beginning (first few bytes) of any file. However, please remember that, if you have a file format that isn’t extremely popular, then it might not work because there’s a chance it might not be in the database.
Anyhow, just to check if it works or not, I renamed one of my ‘.mp4′ video files into ‘.odt’ (open office format) in Ubuntu 12.04. Nautilus tried to open the video using LibreOffice ;-), as shown in the first screenshot. And even under its file properties, it said ‘OpenDocument Text …’.
Then I used ‘file’ and it correctly guessed its actual format.
I did test it with few other popular file types and it worked in all those occasions, except this once, where it failed to identify the file type created by ‘ccrypt‘ (again, probable because it isn’t that popular).
How to use it?
It’s extremely simple and you don’t even have to install it manually (comes installed by default). Open your Terminal window and use it in the below format.
file --mime-type your-file
Replace ‘your-file’ with your file and its path. If it can, then it’ll display the actual name and type of that file as shown above.
If you want to get few more additional info then use it in the below format.
file -i your-file
It has few other options that might be useful. For that, please enter the below command and it’ll display some of its options in brief.
That’s it. Enjoy!.