在 Debian / Ubuntu GNU/Linux 底下找出特定命令 / 程式的來源套件

在 Debian / Ubuntu based GNU/Linux 底下做事有個很棒的優點,有太多工具可以透過 apt 套件庫來進行安裝,重灌電腦的時候只要透過 apt 或 apt-get 就可以把一大半會需要用到的工具給裝起來,不過有時候可能邊摸索邊上網查資料來看某工具要怎麼用,一骨腦裝完卻沒作筆記,日後要準備重灌時卻想不起來該裝什麼套件才能把要用到的命令找回來,窘 …

如果你有安裝 command-not-found 這個套件,在 bash shell 底下如果執行了找不到的命令,除了常見的 blah: command not found,他還會有很貼心的提示使用者應該安裝哪個缺漏的套件,像這樣:

$ kvm
 The program 'kvm' is currently not installed. You can install it by typing:
 sudo apt install qemu-kvm

上面範例為我執行了一個不存在的命令 – kvm ,然後 command-not-found 這套工具建議我去裝 qemu-kvm 這個套件,裝完後就會有 kvm 這個命令可以使用了,如果同時存在不只一個套件包含了同樣的命令,command-not-found 這套工具一樣會很好心的幫我們列出來,例如:

$ sar
 The program 'sar' can be found in the following packages:
  * sysstat
  * atsar
 Ask your administrator to install one of them

就算是找不到 100% 符合的命令,command-not-found 也會很好心的列出其他參考,例如:

$ gitx
 No command 'gitx' found, did you mean:
  Command 'gitg' from package 'gitg' (universe)
  Command 'git' from package 'git' (main)
  Command 'gitk' from package 'gitk' (main)
 gitx: command not found

不過上述的功能僅限於我們沒有正確的安裝對應的套件才能使用,如果是套件已經裝起來了的時候呢?例如我工具都已經裝好了,只是想確認某個命令是從哪裡來的,要做個筆記而已,這種情況很常見的啊,例如遇到問題跑去問 Google ,看到一堆解法,一個不行換一個,換到可以的解法的時候可能已經 apt-get install 不知道多少次了,已經忘記剛剛的命令到底是從哪邊來的了XD

還好 dpkg 有個強大的搜尋功能,可以讓我們從已經安裝的套件中搜尋出他們到底包含了哪些檔案,底下是從 Ubuntu 16.04.1 裏面擷取的 dpkg manpage:

-S, –search filename-search-pattern…
Search for a filename from installed packages.

使用方式為:

$ dpkg -S '檔案路徑/名稱'

要透過 dpkg -S 來找命令對應所屬的套件,只要把命令的完整路徑丟給他就可以了,命令的完整路徑我們可以透過 type 或 which 這兩個指令來確認,例如:

$ type whoami
whoami is /usr/bin/whoami
$ which whoami
/usr/bin/whoami

從上面的輸出我們可以知道 whoami 這個命令的完整路徑是 /usr/bin/whoami ,再把 /usr/bin/whoami 拿去 dpkg 查詢就可以了!例如:

$ dpkg -S '/usr/bin/whoami'
coreutils: /usr/bin/whoami
$ dpkg -S '/usr/bin/curl'
curl: /usr/bin/curl

或是直接

$ dpkg -S "$(which whoami)"
coreutils: /usr/bin/whoami
$ dpkg -S "$(which curl)"
curl: /usr/bin/curl

從上面的結果我們可以發現 curl 這個命令是從 curl 這個套件來的, whoami 這個命令則是從 coreutils 這個套件來的,如果你查詢的是還沒有安裝或是不是透過 apt / dpkg 套件管理工具安裝的命令,那就沒辦法了:

$ dpkg -S '/usr/bin/kvm'
dpkg-query: no path found matching pattern /usr/bin/kvm

要注意的是

1. which 跟 type 輸出的格式不太一樣,type 的輸出結果因為多了 blah is … ,所以還需要經過處理把前面那段拿掉才有辦法餵給 dpkg ,而 which 因為是直接出出路徑所以沒有這問題

2. 這邊如果輸入的不是完整路徑,則可能會出現一堆其他不相關的東西,例如:

$ dpkg -S 'whoami'
 bash-completion: /usr/share/bash-completion/completions/ldapwhoami
 coreutils: /usr/share/man/man1/whoami.1.gz
 coreutils: /usr/bin/whoami

因為 dpkg 會直接把所有完整路徑裏面符合輸入字串的部份都印出來,所以像是 whoami 的 manpage 以及 bash-completion 的自動補齊指令都被列出來了。

好了,這篇筆記雖然短,可我覺得很實用,哪天失憶或是被問問題的時候就可以把這篇丟出來,這就是做筆記最大的用途 … :D

dpkg: error: parsing file ‘/var/lib/dpkg/available’ near line N

好像是因為上一次用apt-get在安裝套件的時候意外中斷造成的

剛剛在裝irssi來掛IRC的時候就卡在這…


peterdavehello@vm-0:~$ sudo apt-get install irssi
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libperl5.14
Suggested packages:
irssi-scripts
The following NEW packages will be installed:
irssi libperl5.14
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/902 kB of archives.
After this operation, 2,446 kB of additional disk space will be used.
Do you want to continue [Y/n]?
dpkg: error: parsing file '/var/lib/dpkg/available' near line 63:
missing package name
E: Sub-process /usr/bin/dpkg returned an error code (2)

解法:
peterdavehello@vm-0:~$ sudo dpkg --clear-avail
搞定收工!

Debian / Ubuntu / LinuxMint /Deepin … dpkg忽略相依性強制移除

用dpkg –remove (-r) 移除套件的時候如果會造成相依性問題預設就不會做處理

看了一下dpkg --help, 應該是可以透過–force來幫忙(!?)

又看了一下dpkg --force-help…有這些說明

dpkg forcing options - control behaviour when problems found:
warn but continue: --force-,,...
stop with error: --refuse-,,... | --no-force-,...
Forcing things:
[!] all Set all force options
[*] downgrade Replace a package with a lower version
configure-any Configure any package which may help this one
hold Process incidental packages even when on hold
not-root Try to (de)install things even when not root
bad-path PATH is missing important programs, problems likely
bad-verify Install a package even if it fails authenticity check
bad-version Process even packages with wrong versions
overwrite Overwrite a file from one package with another
overwrite-diverted Overwrite a diverted file with an undiverted version
[!] overwrite-dir Overwrite one package's directory with another's file
[!] unsafe-io Do not perform safe I/O operations when unpacking
[!] confnew Always use the new config files, don't prompt
[!] confold Always use the old config files, don't prompt
[!] confdef Use the default option for new config files if one
is available, don't prompt. If no default can be found,
you will be prompted unless one of the confold or
confnew options is also given
[!] confmiss Always install missing config files
[!] confask Offer to replace config files with no new versions
[!] architecture Process even packages with wrong or no architecture
[!] breaks Install even if it would break another package
[!] conflicts Allow installation of conflicting packages
[!] depends Turn all dependency problems into warnings
[!] depends-version Turn dependency version problems into warnings
[!] remove-reinstreq Remove packages which require installation
[!] remove-essential Remove an essential package

WARNING - use of options marked [!] can seriously damage your installation.
Forcing options marked [*] are enabled by default.

原先加上--force-breaks無效, 加了--force-depens就對了!