如何列出 Ubuntu 與 Debian 套件(*.deb 檔)的檔案內容?

這裡介紹如何在安裝 Debian 或 Ubuntu 套件(*.deb 檔)之前,先查看套件檔的內容,檢查是否有自己需要的函式庫或執行檔等。


在 Debian/Ubuntu 等 Linux 系統中,所有系統收錄的套件都會被包裝成 deb 檔的格式,安裝時就直接安裝這一個檔就可以了,省去自行下載、解壓縮、編譯與安裝的過程,套件統一管理,所以要移除套件也很方便。

使用 deb 檔來安裝雖然很方便,但是如果我們想知道一個套件到底安裝了哪些檔案,或是想確認一個套件之中到底有沒有包含自己需要的執行檔或是函式庫時,光看一個 deb 檔的名稱是不夠的,這時候就要透過一些方式將 deb 檔的內容列出來才行。

以下介紹兩個方式,可以讓你在安裝之前先查看套件內容。

使用 apt-file 指令查詢

apt-file 是一個 apt 套件的檔案搜尋工具,專門用來搜尋 deb 套件中的檔案。在 Ubuntu 或 Debian 中若要使用它,可以使用 apt-get 安裝:
sudo apt-get install apt-file
在第一次使用之前,要先更新資料庫,讓所有套件的資訊與資料庫中的紀錄同步:
sudo apt-file update
然後就可以查詢指定套件中包含哪些檔案,例如查詢 tofrodos 這個套件的檔案:
apt-file show tofrodos
輸出為:
tofrodos: /usr/bin/fromdos
tofrodos: /usr/bin/todos
tofrodos: /usr/share/doc/tofrodos/NEWS.Debian.gz
tofrodos: /usr/share/doc/tofrodos/changelog.Debian.gz
tofrodos: /usr/share/doc/tofrodos/copyright
tofrodos: /usr/share/doc/tofrodos/readme.txt.gz
tofrodos: /usr/share/doc/tofrodos/tofrodos.html
tofrodos: /usr/share/man/man1/fromdos.1.gz
tofrodos: /usr/share/man/man1/todos.1.gz
由這個輸出我們就可以知道這個 deb 套件中包含哪些檔案,另外也可以看出每個檔案的安裝位置。

解壓縮 deb 套件檔

上面使用 apt-file 指令來查詢的方法雖然很方便,但是它只能看到套件裡面的每個檔案名稱與路徑,如果想看這些檔案的實際內容,就要真的把套件下載下來了,並解壓縮來看了。

要下載 repository 中的 deb 套件檔很簡單,只要使用 apt-get 指令配合 --download-only 這個參數即可:
sudo apt-get --download-only install tofrodos
當你執行這行指令之後,tofrodos 這個套件的 deb 檔就會被下載下來,存放在 /var/cache/apt/archives 目錄中,但是由於我們加入了 --download-only 這個參數,所以 apt-get 不會安裝這個套件,只會單純下載與儲存而已。

接著我們就可以使用 dpkg 指令配合 -c 參數來查看其中的內容:
dpkg -c /var/cache/apt/archives/tofrodos_1.7.9.debian.1-1build1_amd64.deb
輸出為:
drwxr-xr-x root/root 0 2012-10-09 07:18 ./
drwxr-xr-x root/root 0 2012-10-09 07:18 ./usr/
drwxr-xr-x root/root 0 2012-10-09 07:18 ./usr/bin/
-rwxr-xr-x root/root 14680 2012-10-09 07:18 ./usr/bin/fromdos
drwxr-xr-x root/root 0 2012-10-09 07:18 ./usr/share/
drwxr-xr-x root/root 0 2012-10-09 07:18 ./usr/share/doc/
drwxr-xr-x root/root 0 2012-10-09 07:18 ./usr/share/doc/tofrodos/
-rw-r--r-- root/root 2819 2012-10-09 07:18 ./usr/share/doc/tofrodos/changelog.Debian.gz
-rw-r--r-- root/root 1406 2012-10-09 07:18 ./usr/share/doc/tofrodos/copyright
-rw-r--r-- root/root 5410 2011-02-27 17:25 ./usr/share/doc/tofrodos/readme.txt.gz
-rw-r--r-- root/root 5832 2011-02-27 17:25 ./usr/share/doc/tofrodos/tofrodos.html
-rw-r--r-- root/root 391 2012-10-09 07:18 ./usr/share/doc/tofrodos/NEWS.Debian.gz
drwxr-xr-x root/root 0 2012-10-09 07:18 ./usr/share/man/
drwxr-xr-x root/root 0 2012-10-09 07:18 ./usr/share/man/man1/
-rw-r--r-- root/root 1677 2012-10-09 07:18 ./usr/share/man/man1/fromdos.1.gz
lrwxrwxrwx root/root 0 2012-10-09 07:18 ./usr/bin/todos -> fromdos
lrwxrwxrwx root/root 0 2012-10-09 07:18 ./usr/share/man/man1/todos.1.gz -> fromdos.1.gz
這個部份跟使用 apt-get 是差不多的。

另外,因為我們已經把 deb 檔整個下載下來了,所以其實我們可以直接使用 dpkg 指令配合 -x 參數把檔案內容解壓縮出來看,由於解壓縮之後,所有的檔案會按照正常的目錄結構放置,為了避免跟系統中其他的檔案混淆,所以我們先建立一個 tofrodos-deb 資料夾:
mkdir tofrodos-deb
接著把 deb 檔解壓縮:
dpkg -x /var/cache/apt/archives/tofrodos_1.7.9.debian.1-1build1_amd64.deb tofrodos-deb
再看看所有的檔案:
ls -R tofrodos-deb
輸出為:
tofrodos-deb:
usr

tofrodos-deb/usr:
bin share

tofrodos-deb/usr/bin:
fromdos todos

tofrodos-deb/usr/share:
doc man

tofrodos-deb/usr/share/doc:
tofrodos

tofrodos-deb/usr/share/doc/tofrodos:
changelog.Debian.gz copyright NEWS.Debian.gz readme.txt.gz tofrodos.html

tofrodos-deb/usr/share/man:
man1

tofrodos-deb/usr/share/man/man1:
fromdos.1.gz todos.1.gz
這樣就可以直接看到套件中的實際檔案內容了。

參考資料:Xmodulo
本站已經搬家了,欲查看最新的文章,請至 G. T. Wang 新網站

沒有留言:

張貼留言