When you rename files with a different extension, most of the time it does not work. But, if you have a .png file, you can just rename image.png to image.jpeg or image.gif, and it automatically gets converted to the other format and works perfectly fine. Is there some similarity between these formats which allows this to be done? I noticed this a few years back, and got interested when I was trying to convert a .dvi file to a .pdf file; just renaming it worked!
- 72,312
- 2,927
4 Answers
This is what probably happens. The application designated to open files with the file types you mention, is the same across all of those. .gif, .jpg, .png: these are all handled by EOG (GNOME's Eye Of Gnome). I suspect this application does not determine the file type based on the extension, rather it seeks for the magic number.
Note that the behavior of EOG may very well be exceptional, and also beware that no actual conversion is done. It's just that Linux applications tend to rely on different detection methods than file extensions, though many modern desktops actually look for the file extension to determine what application to open, since this method is probably quicker.
This all may apply to Ubuntu, Windows is a completely different story. Explorer practices a behavior by default that cuts off the file extension from the filename. This way, a file named "document.pdf" will show as "document", and renaming it to "document.dvi" will actually be processed as "document.dvi.pdf". Viruses sometimes exploit this "feature" by making the user believe they are opening a .jpg, while in reality they are being tricked into running a malicious exe!
To be really sure about filetypes, there is a command called file that shows you detailed file information.
From my personal experience, I can tell that, at least on my system, EOG cannot be tricked into recognizing a renamed image file. You must be doing something different than I do.
- 17,371
Renaming .png files to .jpeg and .gif works because the program associated with and assigned to open those files is the same, and when it sees the extensions it can open, it simply opens them, because those are all image files and the program can open them.
The default program is EOG (Eye of Gnome) as another answerer says. Even if you remove the extension, it can still open them.
As for the dvi and pdf, again both file types are opened by the same program, which is called evince. So, the same thing happens there.
I am not aware of a file conversion taking place automatically. Most likely, the application used worked around the problem or an application automatically converted the file.
On the command line, you can also use file to give some basic information about the file, e.g.
cp test.png test.png.jpg
file test.png.jpg
Output:
test.png.jpg: PNG image data, 557 x 237, 8-bit/color RGB, non-interlaced
See the file manpage:
man file
You can use convert (it's in the package imagemagick or rather imagemagick-6.q16) or some other tool to convert the file.
convert test.png test.jpg
file test.jpg
Output:
test.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 557x237, components 3
Some tools may not be affected by the "wrong" file extensions (because they ignore them) as explained in the accepted answer, but I would not rely on this to always work.
Tip: Using file you can create a simple script to search for files with the wrong file format.
- 135
Is there some similarity between these formats which allows this to be done?
Although some similarities exist between the two file formats, the main point is that the extension in the filename does not always represent the filetype its describing. In Linux this is a given since you always specify the program you want to open your file with.
You can even check if there really is any difference between the two files comparing them using the GNU cmp utility together with the --verbose --bytes=100 options to limit and view the differing bytes. I also encourage you to display the header on your terminal through less to not overflow it, or even better, head -c 100 to only view the first 100 bytes to get a grasp of what cmp and your OS is actually reading. Neither in Ubuntu nor Windows I've been able to spot a difference. Using Paint on Windows to save a PNG to JPG format indeed changed the header from PNG to JFIF, and subsequently its contents.
I'm personally more familiar with the Windows filesystem. The extension is there so the OS can identify faster the program it is associated with, and can be modified by the user by choosing default program when selecting "Open with..." on the context menu. You can also view all the extensions Windows recognizes from the programs it has installed by launching regedit and navigating to HKCR\.txt to investigate how Windows perceives the "txt" file extension. I've personally used the Registry Editor to add the string Content Type with value text/plain to different extensions to be able to preview the text contents of a file on the Windows File Explorer's Preview Pane.
Please don't hate on me for talking about Windows. I just thought it was interesting to point out since many Windows users don't know the inner working of their Windows machine.