Why is it possible to convert a file just by renaming its extension?

40,921

Solution 1

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.

Solution 2

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.

Share:
40,921

Related videos on Youtube

notablytipsy
Author by

notablytipsy

Updated on September 18, 2022

Comments

  • notablytipsy
    notablytipsy over 1 year

    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!

    • Web-E
      Web-E almost 12 years
      png to jpeg doesn't work in my system. The format is completely different. Are you sure that happens in ubuntu!! :O
    • Deepak Verma
      Deepak Verma almost 12 years
      No, there is absolutely no similarity whatsoever between gif and jpeg. You cannot simply rename any of the files to convert them, except by using an app which does it for you. For example, saving a gif file as jpeg in something like Gimp will convert it; renaming it on a command line will not.
    • notablytipsy
      notablytipsy almost 12 years
      I am renaming from the command line, and yes, it does work. I am using Ubuntu only, and I remember it working even on Windows.
    • notablytipsy
      notablytipsy almost 12 years
      What about dvi and pdf? Are there similarities between those two?
    • notablytipsy
      notablytipsy about 11 years
      Reading this a few months later, I feel very foolish asking this question.
  • notablytipsy
    notablytipsy almost 12 years
    I used Windows XP, and I always had the file type shown. Anyway, I guess that does make sense. Thanks! And I did not know about that Windows feature of cutting file extensions ... Good thing I use Ubuntu now ;) And thanks for the information about magic numbers. I knew what they were, but didn't know they were called "magic numbers"
  • ImaginaryRobots
    ImaginaryRobots almost 12 years
    neither windows nor ubuntu convert files just by renaming them - an overly smart program is just ignoring the extension completely when loading the file.
  • Nmath
    Nmath over 3 years
    Ubuntu associates file extensions with default programs for each extension.