在 Linux 中開啟檔案看似簡單,有啟動圖標點一下就好;但若沒有圖標,該如何在終端裡開啟呢?其實一條指令就能搞定。以下教你用指令在 Linux 終端開啟檔案。

在 Linux 環境工作,大部分時間都在與終端機打交道,常需要檢視某個檔案,例如一張圖片或一份 Word 文件(終端裡無法直接查看)。一般做法是切換到圖形介面、進入檔案所在目錄、雙擊開啟。另一種做法是直接在 shell 輸入指定程式,例如 evince xx.pdf 開啟 PDF。但前提是你要記得程式名稱——你記得住用 LibreOffice 開啟 Word 的指令嗎?反正我記不住。
圖形介面其實已定義好各種預設開啟方式,雙擊網頁會用預設瀏覽器開啟、雙擊圖片會用預設檢視器開啟,那麼終端裡應該也有對應機制。順著這個思路搜尋,我發現了 xdg-open 這個指令(相容 GNOME 與 KDE),它會採用與圖形介面一致的預設開啟方式,行為等同直接雙擊。
有了它,在終端開啟檔案就方便多了。例如用預設的 PDF 閱讀器開啟:
xdg-open xxx.pdf
也能用圖形檔案管理器開啟目錄,例如開啟使用者主目錄:
xdg-open ~
非常實用!具體用法:
xdg-open { file | URL }
xdg-open { --help | --manual | --version }
搭配 Ctrl+Alt+T 開啟 shell、F11 全螢幕(Alt+F9 最小化、Alt+F10 最大化/還原),再加上 xdg-open,就能大幅減少滑鼠操作、提升效率。
附 xdg-open 手冊摘要:xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser. If a file is provided the file will be opened in the preferred application for files of that type. xdg-open supports file, ftp, http and https URLs. xdg-open is for use inside a desktop session only. It is not recommended to use xdg-open as root.
以上便是用 xdg-open 在 Linux 終端開啟檔案的方法,能有效減少滑鼠操作,簡單又實用,趕快試試看吧。