如何在 Linux 上安裝/解除安裝一個檔案中列出的軟體包?

Magesh Maruthamuthu發表於2019-06-01

在某些情況下,你可能想要將一個伺服器上的軟體包列表安裝到另一個伺服器上。例如,你已經在伺服器 A 上安裝了 15 個軟體包並且這些軟體包也需要被安裝到伺服器 B、伺服器 C 上等等。

我們可以手動去安裝這些軟體但是這將花費大量的時間。你可以手動安裝一倆個伺服器,但是試想如果你有大概十個伺服器呢。在這種情況下你無法手動完成工作,那麼怎樣才能解決問題呢?

不要擔心我們可以幫你擺脫這樣的情況和場景。我們在這篇文章中增加了四種方法來克服困難。

我希望這可以幫你解決問題。我已經在 Centos7 和 Ubuntu 18.04 上測試了這些命令。

我也希望這可以在其他發行版上工作。這僅僅需要使用該發行版的官方包管理器命令替代本文中的包管理器命令就行了。

如果想要 檢查 Linux 系統上已安裝的軟體包列表,請點選連結。

例如,如果你想要在基於 RHEL 系統上建立軟體包列表請使用以下步驟。其他發行版也一樣。

# rpm -qa --last | head -15 | awk '{print $1}' > /tmp/pack1.txt

# cat /tmp/pack1.txt
mariadb-server-5.5.60-1.el7_5.x86_64
perl-DBI-1.627-4.el7.x86_64
perl-DBD-MySQL-4.023-6.el7.x86_64
perl-PlRPC-0.2020-14.el7.noarch
perl-Net-Daemon-0.48-5.el7.noarch
perl-IO-Compress-2.061-2.el7.noarch
perl-Compress-Raw-Zlib-2.061-4.el7.x86_64
mariadb-5.5.60-1.el7_5.x86_64
perl-Data-Dumper-2.145-3.el7.x86_64
perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64
httpd-2.4.6-88.el7.centos.x86_64
mailcap-2.1.41-2.el7.noarch
httpd-tools-2.4.6-88.el7.centos.x86_64
apr-util-1.5.2-6.el7.x86_64
apr-1.4.8-3.el7_4.1.x86_64

方法一:如何在 Linux 上使用 cat 命令安裝檔案中列出的包?

為實現這個目標,我將使用簡單明瞭的第一種方法。為此,建立一個檔案並新增上你想要安裝的包列表。

出於測試的目的,我們將只新增以下的三個軟體包名到檔案中。

# cat /tmp/pack1.txt

apache2
mariadb-server
nano

只要簡單的執行 apt 命令 就能在 Ubuntu/Debian 系統上一次性安裝所有的軟體包。

# apt -y install $(cat /tmp/pack1.txt)

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libopts25 sntp
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom spell
The following NEW packages will be installed:
  apache2 mariadb-server nano
0 upgraded, 3 newly installed, 0 to remove and 24 not upgraded.
Need to get 339 kB of archives.
After this operation, 1,377 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2 amd64 2.4.29-1ubuntu4.6 [95.1 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 nano amd64 2.9.3-2 [231 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mariadb-server all 1:10.1.38-0ubuntu0.18.04.1 [12.9 kB]
Fetched 339 kB in 19s (18.0 kB/s)
Selecting previously unselected package apache2.
(Reading database ... 290926 files and directories currently installed.)
Preparing to unpack .../apache2_2.4.29-1ubuntu4.6_amd64.deb ...
Unpacking apache2 (2.4.29-1ubuntu4.6) ...
Selecting previously unselected package nano.
Preparing to unpack .../nano_2.9.3-2_amd64.deb ...
Unpacking nano (2.9.3-2) ...
Selecting previously unselected package mariadb-server.
Preparing to unpack .../mariadb-server_1%3a10.1.38-0ubuntu0.18.04.1_all.deb ...
Unpacking mariadb-server (1:10.1.38-0ubuntu0.18.04.1) ...
Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
Setting up apache2 (2.4.29-1ubuntu4.6) ...
Processing triggers for ureadahead (0.100.0-20) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Setting up nano (2.9.3-2) ...
update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
update-alternatives: using /bin/nano to provide /usr/bin/pico (pico) in auto mode
Processing triggers for systemd (237-3ubuntu10.20) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up mariadb-server (1:10.1.38-0ubuntu0.18.04.1) ...

至於刪除,需要使用相同的命令格式和適當的選項。

# apt -y remove $(cat /tmp/pack1.txt)
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apache2-bin apache2-data apache2-utils galera-3 libaio1 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libjemalloc1 liblua5.2-0
  libmysqlclient20 libopts25 libterm-readkey-perl mariadb-client-10.1 mariadb-client-core-10.1 mariadb-common mariadb-server-10.1 mariadb-server-core-10.1 mysql-common sntp socat
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  apache2 mariadb-server nano
0 upgraded, 0 newly installed, 3 to remove and 24 not upgraded.
After this operation, 1,377 kB disk space will be freed.
(Reading database ... 291046 files and directories currently installed.)
Removing apache2 (2.4.29-1ubuntu4.6) ...
Removing mariadb-server (1:10.1.38-0ubuntu0.18.04.1) ...
Removing nano (2.9.3-2) ...
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in auto mode
Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

使用 yum 命令 在基於 RHEL (如 Centos、RHEL (Redhat) 和 OEL (Oracle Enterprise Linux)) 的系統上安裝檔案中列出的軟體包。

# yum -y install $(cat /tmp/pack1.txt)

使用以命令在基於 RHEL (如 Centos、RHEL (Redhat) 和 OEL (Oracle Enterprise Linux)) 的系統上解除安裝檔案中列出的軟體包。

# yum -y remove $(cat /tmp/pack1.txt)

使用以下 dnf 命令 在 Fedora 系統上安裝檔案中列出的軟體包。

# dnf -y install $(cat /tmp/pack1.txt)

使用以下命令在 Fedora 系統上解除安裝檔案中列出的軟體包。

# dnf -y remove $(cat /tmp/pack1.txt)

使用以下 zypper 命令 在 openSUSE 系統上安裝檔案中列出的軟體包。

# zypper -y install $(cat /tmp/pack1.txt)

使用以下命令從 openSUSE 系統上解除安裝檔案中列出的軟體包。

# zypper -y remove $(cat /tmp/pack1.txt)

使用以下 pacman 命令 在基於 Arch Linux (如 Manjaro 和 Antergos) 的系統上安裝檔案中列出的軟體包。

# pacman -S $(cat /tmp/pack1.txt)

使用以下命令從基於 Arch Linux (如 Manjaro 和 Antergos) 的系統中解除安裝檔案中列出的軟體包。

# pacman -Rs $(cat /tmp/pack1.txt)

方法二:如何使用 cat 和 xargs 命令在 Linux 中安裝檔案中列出的軟體包。

甚至,我更喜歡使用這種方法,因為這是一種非常簡單直接的方法。

使用以下 apt 命令在基於 Debian 的系統 (如 Debian、Ubuntu 和 Linux Mint) 上安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs apt -y install

使用以下 apt 命令 從基於 Debian 的系統 (如 Debian、Ubuntu 和 Linux Mint) 上解除安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs apt -y remove

使用以下 yum 命令在基於 RHEL (如 Centos,RHEL (Redhat) 和 OEL (Oracle Enterprise Linux)) 的系統上安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs yum -y install

使用以命令從基於 RHEL (如 Centos、RHEL (Redhat) 和 OEL (Oracle Enterprise Linux)) 的系統上解除安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs yum -y remove

使用以下 dnf 命令在 Fedora 系統上安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs dnf -y install

使用以下命令從 Fedora 系統上解除安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs dnf -y remove

使用以下 zypper 命令在 openSUSE 系統上安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs zypper -y install

使用以下命令從 openSUSE 系統上解除安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs zypper -y remove

使用以下 pacman 命令在基於 Arch Linux (如 Manjaro 和 Antergos) 的系統上安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs pacman -S

使用下以命令從基於 Arch Linux (如 Manjaro 和 Antergos) 的系統上解除安裝檔案中列出的軟體包。

# cat /tmp/pack1.txt | xargs pacman -Rs

方法三 : 如何使用 For 迴圈在 Linux 上安裝檔案中列出的軟體包

我們也可以使用 for 迴圈命令來實現此目的。

安裝批量包可以使用以下一條 for 迴圈的命令。

# for pack in `cat /tmp/pack1.txt` ; do apt -y install $i; done

要使用 shell 指令碼安裝批量包,請使用以下 for 迴圈。

# vi /opt/scripts/bulk-package-install.sh

#!/bin/bash
for pack in `cat /tmp/pack1.txt`
do apt -y remove $pack
done

bulk-package-install.sh 設定可執行許可權。

# chmod + bulk-package-install.sh

最後執行這個指令碼。

# sh bulk-package-install.sh

方法四:如何使用 While 迴圈在 Linux 上安裝檔案中列出的軟體包

我們也可以使用 while 迴圈命令來實現目的。

安裝批量包可以使用以下一條 while 迴圈的命令。

# file="/tmp/pack1.txt"; while read -r pack; do apt -y install $pack; done < "$file"

要使用 shell 指令碼安裝批量包,請使用以下 while 迴圈。

# vi /opt/scripts/bulk-package-install.sh

#!/bin/bash
file="/tmp/pack1.txt"
while read -r pack
do apt -y remove $pack
done < "$file"

bulk-package-install.sh 設定可執行許可權。

# chmod + bulk-package-install.sh

最後執行這個指令碼。

# sh bulk-package-install.sh

via: https://www.2daygeek.com/how-to-install-uninstall-listed-packages-from-a-file-in-linux/

作者:Magesh Maruthamuthu 選題:lujun9972 譯者:way-ww 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

如何在 Linux 上安裝/解除安裝一個檔案中列出的軟體包?

訂閱“Linux 中國”官方小程式來檢視

相關文章