Linux:桌面系统中的文件后缀和类型

日晨难再 2024-06-23 16:37:03 阅读 91

相关阅读

Linux

icon-default.png?t=N7T8

https://blog.csdn.net/weixin_45791458/category_12234591.html?spm=1001.2014.3001.5482


        Linux中的文件后缀与Windows系统有些不同,因为其似乎没有很重要,一个文件是否可执行对后缀没有要求。但是,后缀依然可以用于表示文件类型,也可以据此选择不同类型文件的默认打开方式。

        在Linux系统中,MIME(多用途互联网邮件扩展)文件类型机制是用来识别和处理不同类型文件的一种方式。MIME类型,也称为互联网媒体类型,最初是为了在电子邮件系统中识别不同格式的文件而设计的,但后来被广泛应用于HTTP传输中以及在操作系统如Linux中识别文件格式。

        MIME类型通常由两部分组成,类型(type)和子类型(subtype),用斜线分隔。例如,test/html表示HTML文档,image/jpeg表示JPEG图像文件。

        /usr/share/mime目录中有一些XML文件,它们是自动生成的,在安装或更新shared-mime-info包时由update-mime-database命令更新,它们规定了MIME类型、以及文件后缀和文件类型的映射关系。

        /usr/share/mime目录下的types文件中包括了目前的所有MIME类型,如下所示。

***********************text/xmcdvideo/3gppvideo/3gpp2video/annodexvideo/dvvideo/isivideovideo/mp2tvideo/mp4video/mpegvideo/oggvideo/quicktimevideo/vnd.mpegurlvideo/vnd.rn-realvideovideo/vnd.vivovideo/waveletvideo/webmvideo/x-animvideo/x-flic***********************

        下面用/usr/share/mime/text目录下x-python.xml文件进行举例说明。

<?xml version="1.0" encoding="utf-8"?><mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="text/x-python"> <!--Created automatically by update-mime-database. DO NOT EDIT!--> <comment>Python script</comment> <comment xml:lang="ar">سكربت بايثون</comment> <comment xml:lang="be@latin">Skrypt Python</comment> <comment xml:lang="bg">Скрипт — Python</comment> <comment xml:lang="ca">script Python</comment> <comment xml:lang="cs">skript Python</comment> <comment xml:lang="da">Pythonprogram</comment> <comment xml:lang="de">Python-Skript</comment> <comment xml:lang="el">Δέσμη ενεργειών Python</comment> <comment xml:lang="en_GB">Python script</comment> <comment xml:lang="eo">Python-skripto</comment> <comment xml:lang="es">secuencia de órdenes en Python</comment> <comment xml:lang="eu">Python script-a</comment> <comment xml:lang="fi">Python-komentotiedosto</comment> <comment xml:lang="fo">Python boðrøð</comment> <comment xml:lang="fr">script Python</comment> <comment xml:lang="ga">script Python</comment> <comment xml:lang="gl">Script en Python</comment> <comment xml:lang="he">תסריט Python</comment> <comment xml:lang="hr">Python skripta</comment> <comment xml:lang="hu">Python-parancsfájl</comment> <comment xml:lang="ia">Script Python</comment> <comment xml:lang="id">Skrip Python</comment> <comment xml:lang="it">Script Python</comment> <comment xml:lang="ja">Python スクリプト</comment> <comment xml:lang="kk">Python сценарийі</comment> <comment xml:lang="ko">파이썬 스크립트</comment> <comment xml:lang="lt">Python scenarijus</comment> <comment xml:lang="lv">Python skripts</comment> <comment xml:lang="ms">Skrip Python</comment> <comment xml:lang="nb">Python-skript</comment> <comment xml:lang="nl">Python-script</comment> <comment xml:lang="nn">Python-skript</comment> <comment xml:lang="oc">escript Python</comment> <comment xml:lang="pl">Skrypt Python</comment> <comment xml:lang="pt">script Python</comment> <comment xml:lang="pt_BR">Script Python</comment> <comment xml:lang="ro">Script Python</comment> <comment xml:lang="ru">сценарий Python</comment> <comment xml:lang="sk">Skript Python</comment> <comment xml:lang="sl">Skriptna datoteka Python</comment> <comment xml:lang="sq">Script Python</comment> <comment xml:lang="sr">Питонова скрипта</comment> <comment xml:lang="sv">Pythonskript</comment> <comment xml:lang="tr">Python betiği</comment> <comment xml:lang="uk">скрипт мовою Python</comment> <comment xml:lang="vi">Văn lệnh Python</comment> <comment xml:lang="zh_CN">Python 脚本</comment> <comment xml:lang="zh_TW">Python 指令稿</comment> <sub-class-of type="application/x-executable"/> <sub-class-of type="text/plain"/> <glob pattern="*.py"/> <glob pattern="*.pyx"/> <glob pattern="*.wsgi"/></mime-type>

        XML文件中类似<comment>Python script</comment>的字段,规定了在桌面系统下,text/x-python类型文件在进行查询时显示的类型。最后的<glob pattern="*.py"/>字段,规定了哪些后缀的文件会被识别为text/x-python类型,不加说明后缀是大小写不敏感的,即.py、.PY、.pY和.Py都会被识别为text/x-python类型。

        下面给出了一个大小写敏感的例子,出现在x-c++src.xml中,其中的<glob pattern="*.C" case-sensitive="true"/>字段说明了只有大写C后缀的文件才会被识别为text/x-c++src类型

<?xml version="1.0" encoding="utf-8"?><mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="text/x-c++src"> <!--Created automatically by update-mime-database. DO NOT EDIT!--> <comment>C++ source code</comment> <comment xml:lang="ar">شفرة مصدر سي++</comment> <comment xml:lang="be@latin">Kryničny kod C++</comment> <comment xml:lang="bg">Изходен код — C++</comment> <comment xml:lang="ca">codi font en C++</comment> <comment xml:lang="cs">zdrojový kód C++</comment> <comment xml:lang="da">C++-kildekode</comment> <comment xml:lang="de">C++-Quelltext</comment> <comment xml:lang="el">Πηγαίος κώδικας C++</comment> <comment xml:lang="en_GB">C++ source code</comment> <comment xml:lang="eo">C++-fontkodo</comment> <comment xml:lang="es">código fuente en C++</comment> <comment xml:lang="eu">C++ iturburu-kodea</comment> <comment xml:lang="fi">C++-lähdekoodi</comment> <comment xml:lang="fo">C++ keldukota</comment> <comment xml:lang="fr">code source C++</comment> <comment xml:lang="ga">cód foinseach C++</comment> <comment xml:lang="gl">código fonte de C++</comment> <comment xml:lang="he">קוד מקור של C++‎</comment> <comment xml:lang="hr">C++ izvorni kod</comment> <comment xml:lang="hu">C++-forráskód</comment> <comment xml:lang="ia">Codice-fonte C++</comment> <comment xml:lang="id">Kode program C++</comment> <comment xml:lang="it">Codice sorgente C++</comment> <comment xml:lang="ja">C++ ソースコード</comment> <comment xml:lang="ka">C++-ის საწყისი კოდი</comment> <comment xml:lang="kk">C++ бастапқы коды</comment> <comment xml:lang="ko">C++ 소스 코드</comment> <comment xml:lang="lt">C++ pradinis kodas</comment> <comment xml:lang="lv">C++ pirmkods</comment> <comment xml:lang="ms">Kod sumber C++</comment> <comment xml:lang="nb">C++-kildekode</comment> <comment xml:lang="nl">C++-broncode</comment> <comment xml:lang="nn">C++-kjeldekode</comment> <comment xml:lang="oc">còde font C++</comment> <comment xml:lang="pl">Kod źródłowy C++</comment> <comment xml:lang="pt">código origem C++</comment> <comment xml:lang="pt_BR">Código-fonte C++</comment> <comment xml:lang="ro">Cod sursă C++</comment> <comment xml:lang="ru">исходный код C++</comment> <comment xml:lang="sk">Zdrojový kód jazyka C++</comment> <comment xml:lang="sl">Datoteka izvorne kode C++</comment> <comment xml:lang="sq">Kod burues C++</comment> <comment xml:lang="sr">Ц++ изворни ко̂д</comment> <comment xml:lang="sv">C++-källkod</comment> <comment xml:lang="tr">C++ kaynak kodu</comment> <comment xml:lang="uk">вихідний код мовою C++</comment> <comment xml:lang="vi">Mã nguồn C++</comment> <comment xml:lang="zh_CN">C++ 源代码</comment> <comment xml:lang="zh_TW">C++ 源碼</comment> <sub-class-of type="text/x-csrc"/> <glob pattern="*.cpp"/> <glob pattern="*.cxx"/> <glob pattern="*.cc"/> <glob pattern="*.C" case-sensitive="true"/> <glob pattern="*.c++"/></mime-type>

        下图展示了在桌面系统GNOME中,一个.py后缀文件的类型查询结果。 

        可以看出首先根据系统语言(这里是默认),显示了<comment>字段规定的类型名,并且在小括号中显示了MIME类型。

        不同的桌面系统的显示结果可能有差异,下面是桌面系统XFCE中,一个.py后缀文件的类型查询结果。

        其中根据系统语言(这里是zh_CN),显示了<comment>字段规定的类型名,即Python 脚本。

        需要注意的是,一个有后缀的文件,显示出来的类型是由后缀决定的,这可能与文件真实类型不符合。如一个可执行二进制文件,如果重命名以.py为后缀,则其显示出来的类型会是错误的Python 脚本。所以一个文件的后缀只是对其类型的一个暗示,但并不确保其准确性。如果要准确识别一个文件的类型,可以使用file命令,它是通过文件的魔术头识别文件类型的,因此更为准确,如下所示。

$ file test.pytest.py: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=8c8cfe14852611f912f695c4ff7fae0b6607382d, not stripped

        对于没有后缀的文件,桌面系统会根据文件内容尝试推断其类型,即如果他是一个二进制文件,则可能推断为executable (application/x-executable)类型。下面是一个例子展示了一个没有后缀的jupyter+json格式的文件被正确识别为了Jupyter Notebook (application/x-ipynb+json)类型。

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 简单的Jupyter Notebook" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "print('Hello, world!')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2}



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。