Results 1 to 5 of 5

Thread: HOWTO: change how Nautilus parses file types

  1. #1
    Join Date
    Oct 2007
    Beans
    188

    HOWTO: change how Nautilus parses file types

    After searching around these forums a bit I finally found a solution to problems I was having with Nautilius.

    The problem:

    Nautilus reads files in order to determine the file type, rather than basing it off the extension. While in theory this is a great idea, it fails in a couple of places. Notably with plain text files, many scripts/programs are parsed incorrectly. For example, a PHP script is only parsed as such if the first line contains <?php which is not always the case. This causes a few unwanted effects like opening with different apps by default and files jumping around in the file list window when ordered by type.

    The solution: Change how Nautilus parses the file types! The rules it uses are all listed in the file /usr/share/mime/packages/freedesktop.org.xml. I've provided two solutions: the former for people familiar with XML and Linux in general; the letter being more detailed.

    Quick solution:

    1. Back up the freedesktop.org.xml.
    2. Open the freedesktop.org.xml file as super-user.
    3. Find the mime-type that your file is being incorrectly parsed as.
    4. Remove the <magic> element and its contents, or alternatively change the <match> elements.
    5. Run sudo update-mime-database /usr/share/mime

    Detailed solution:

    Don't forget to back up first:
    Code:
    sudo cp /usr/share/mime/packages/freedesktop.org.xml /usr/share/mime/packages/freedesktop.org.xml.bak
    You can open the file with the command (& is so you can continue using commands with the file open)
    Code:
    sudo gedit /usr/share/mime/packages/freedesktop.org.xml &
    If you search for "*.php", you should see a section similar to this. To keep it short I've removed the translations but you should leave them in the file.
    Code:
      <mime-type type="application/x-php">
        <sub-class-of type="text/plain"/>
        <comment>PHP script</comment>
        <comment xml:lang="az">PHP skripti</comment>
        [... more translations here ...]
        <magic priority="80">
          <match value="&lt;?php" type="string" offset="0:64"/>
        </magic>
        <glob pattern="*.php"/>
        <glob pattern="*.php3"/>
        <glob pattern="*.php4"/>
      </mime-type>
    The part in bold is what will decide the type. For each mime type, it is listed in <magic> tags. So for your chosen file type, all you have to do is remove the <magic> element and its contents.

    Once you're done, save the file and run this command:
    Code:
    sudo update-mime-database /usr/share/mime
    The same process should apply for all file types. To stop PHP files being parsed as HTML documents, you will need to remove the magic element from the HTML mime type as well.

    Hope this is useful to people!
    "Maybe I shouldn't have eaten that packet of powdered gravy I found in the parking lot" -- Homer Simpson
    My sites: Simpson Crazy // Toon pictures // CodeLair // Instant Song Lyrics

  2. #2
    Join Date
    Feb 2008
    Beans
    5

    Re: HOWTO: change how Nautilus parses file types

    Thanks for the awesome walkthough! Ubuntu kept warning me that my Javascript files were really MATLAB files trying to secretly pwn me. Your tip did the trick!

    :-j
    Josh

  3. #3
    Join Date
    Apr 2008
    Beans
    22

    Re: HOWTO: change how Nautilus parses file types

    Likewise - thanks! I recently ported from Windows XP, so am very new to Ubuntu, and trying to get a handle. A lot of my ported files where showing up as mime-type x-vhdl, a very wierd file type "Very High Speed Integrated Circuit." Someone was definitely very high when they included the "magic" in the freedesktop.org.xml so that files having a "--" show up as this file type. This can be really confusing for people coming over to Ubuntu and Nautilus - how can this misapplication be reported so its removed from future versions?? Also, the sudo update-mime-database didn't change file types on the fly, but rebooting did; am on Ubuntu 7.10.

  4. #4
    Join Date
    Jul 2007
    Location
    Sweden
    Beans
    24

    Re: HOWTO: change how Nautilus parses file types

    thanks so much for posting that!

  5. #5
    Join Date
    Sep 2005
    Beans
    3

    Re: HOWTO: change how Nautilus parses file types

    Is there some way to totally disable the mimetype "magic" that is going on? I would like things to just open based on file extension, even if I might open a mislabeled file.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •