Contents
  1. 1. MIME type
    1. 1.1. 設定方式(default.list and mimeapps.list)
  2. 2. Command
  3. 3. Reference

針對不同檔案類型,常常存有許多同性質的應用程式,例如文字檔,Linux上就有vim, emacs, gedit等等,如果有一個方法可以指定各型檔案的預設應用程式(default application),則日常操作上會順心很多,在Linux上可以透過 檔案管理器(file manager),MIME type,環境變數來指定預設程式。

MIME type

MIME type現在被稱為Internet Media Types,一開始是為email而設計的,其縮寫原本代表Multipurpose Internet Mail Extensions,但現在延伸到其他應用。

freedesktop.org維護了一份資料庫,紀錄了所有MIME type及檔案類型的對應,例如以下副檔名和MIME type的對應:

1
2
3
4
5
.txt – text/plain
.html – text/html
.mp3 – audio/mpeg3
.png – image/png
.doc – application/msword

在Windows上,檔案需要有副檔名(file extension)才能被正確的打開,否則系統會無法判斷其檔案類型,可是Unix系統即使沒有副檔名一樣能被正確地開啟,主要差異是Windows系統捨棄了MIME type的支援,而Linux的檔案系統會在檔案的檔頭(header or metadata)中儲存檔案的MIME type。

設定方式(default.list and mimeapps.list)

default.listmimeapps.list都是用來紀錄各mime type對應應用程式的設定檔,透過修改$HOME/.local/share/applications/mimeapps.list,可以改變各種預設應用程式,修改後執行update-mime-database ~/.local/share/mime使改變生效。
我的mimeapps.list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
[Default Applications]
text/html=firefox.desktop
x-scheme-handler/http=firefox.desktop
x-scheme-handler/https=firefox.desktop
x-scheme-handler/about=firefox.desktop
x-scheme-handler/unknown=firefox.desktop
x-scheme-handler/ftp=firefox.desktop
x-scheme-handler/chrome=firefox.desktop
application/x-extension-htm=firefox.desktop
application/x-extension-html=firefox.desktop
application/x-extension-shtml=firefox.desktop
application/xhtml+xml=firefox.desktop
application/x-extension-xhtml=firefox.desktop
application/x-extension-xht=firefox.desktop
application/pdf=zathura.desktop;
video/x-ogm+ogg=vlc.desktop
video/dv=vlc.desktop
video/mpeg=vlc.desktop
video/x-mpeg=vlc.desktop
video/msvideo=vlc.desktop
video/quicktime=vlc.desktop
video/x-anim=vlc.desktop
video/x-avi=vlc.desktop
video/x-ms-asf=vlc.desktop
video/x-ms-wmv=vlc.desktop
video/x-msvideo=vlc.desktop
video/x-nsv=vlc.desktop
video/x-flc=vlc.desktop
video/x-fli=vlc.desktop
video/x-flv=vlc.desktop
video/vnd.rn-realvideo=vlc.desktop
video/mp4=vlc.desktop
video/mp4v-es=vlc.desktop
video/mp2t=vlc.desktop
video/x-matroska=vlc.desktop
video/webm=vlc.desktop
image/x-eps=feh.desktop
image/x-ico=feh.desktop
image/x-portable-bitmap=feh.desktop
image/x-portable-graymap=feh.desktop
image/x-portable-pixmap=feh.desktop
image/x-xbitmap=feh.desktop
image/x-xpixmap=feh.desktop
image/tiff=feh.desktop
image/x-psd=feh.desktop
image/x-webp=feh.desktop
image/jpeg=feh.desktop
application/x-trash=gedit.desktop
text/plain=gvim.desktop

[Added Associations]
x-scheme-handler/http=firefox.desktop;
x-scheme-handler/https=firefox.desktop;
x-scheme-handler/ftp=firefox.desktop;
x-scheme-handler/chrome=firefox.desktop;
text/html=firefox.desktop;
application/x-extension-htm=firefox.desktop;
application/x-extension-html=firefox.desktop;
application/x-extension-shtml=firefox.desktop;
application/xhtml+xml=firefox.desktop;
application/x-extension-xhtml=firefox.desktop;
application/x-extension-xht=firefox.desktop;
application/x-bittorrent=transmission-gtk.desktop;
application/pdf=zathura.desktop;
video/x-matroska=vlc.desktop;
video/dv=vlc.desktop;
video/mpeg=vlc.desktop;
video/x-mpeg=vlc.desktop;
video/msvideo=vlc.desktop;
video/quicktime=vlc.desktop;
video/x-anim=vlc.desktop;
video/x-avi=vlc.desktop;
video/x-ms-asf=vlc.desktop;
video/x-ms-wmv=vlc.desktop;
video/x-msvideo=vlc.desktop;
video/x-nsv=vlc.desktop;
video/x-flc=vlc.desktop;
video/x-fli=vlc.desktop;
video/x-flv=vlc.desktop;
video/vnd.rn-realvideo=vlc.desktop;
video/mp4=vlc.desktop;
video/mp4v-es=vlc.desktop;
video/mp2t=vlc.desktop;
video/webm=vlc.desktop;
image/x-eps=feh.desktop
image/x-ico=feh.desktop
image/x-portable-bitmap=feh.desktop
image/x-portable-graymap=feh.desktop
image/x-portable-pixmap=feh.desktop
image/x-xbitmap=feh.desktop
image/x-xpixmap=feh.desktop
image/tiff=feh.desktop
image/x-psd=feh.desktop
image/x-webp=feh.desktop
image/jpeg=feh.desktop;shotwell-viewer.desktop;
inode/directory=thunar.desktop;
application/x-trash=gedit.desktop;
text/plain=gvim.desktop;

Command

  • xdg-mime

    command line tool for querying information about file type handling and adding descriptions for new file types

    1
    2
    3
    4
    $ xdg-mime query default text/plain
    gvim.desktop
    $ xdg-mime query filetype foo.txt
    text/plain
  • file

    1
    2
    $ file --mime-type foo.txt
    text/plain
  • stat - display file or file system status

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $ stat ~/test/test.c
    File: ‘/home/jordan/test/test.c’
    Size: 277 Blocks: 8 IO Block: 4096 regular file
    Device: 802h/2050d Inode: 787434 Links: 1
    Access: (0664/-rw-rw-r--) Uid: ( 1000/ jordan) Gid: ( 1000/ jordan)
    Access: 2016-04-08 23:22:13.469538594 +0800
    Modify: 2016-04-08 23:22:11.485538674 +0800
    Change: 2016-04-08 23:22:11.489538673 +0800
    Birth: -

    $ stat -f /
    File: "/"
    ID: 6660e78ba44d9d99 Namelen: 255 Type: ext2/ext3
    Block size: 4096 Fundamental block size: 4096
    Blocks: Total: 7177215 Free: 3561879 Available: 3191537
    Inodes: Total: 1831424 Free: 1346725

Reference

MIME Types Explained: Why Linux and Mac OS X Don’t Need File Extensions
Cannot set default application for text/plain
Arch wiki - Default applications

Contents
  1. 1. MIME type
    1. 1.1. 設定方式(default.list and mimeapps.list)
  2. 2. Command
  3. 3. Reference