How to install on Linux / uninstall a package listed in the file?

In some cases, you may want to install packages on a list server to another server. For example, you have installed on the server A 15 packages and these packages also need to be installed in the server B, C, etc. on the server.

We can manually to install the software but it will take a lot of time. You can manually install a two servers, but imagine if you have about ten server yet. In this case you can not manually complete the work, then how can we solve the problem?

Do not worry we can help you get out of this situation and scenarios. We have added four methods in this article to overcome difficulties.

I also hope it can work on other distributions. It only requires the release of the official package manager command replace paper package manager command line.

If you want to check the list of packages on a Linux system already installed , please click on the link.

For example, if you want to create a list of packages based on RHEL systems use the following procedure. Other distributions, too.

  1. # rpm -qa --last|head-15| awk '{print $1}'>/tmp/pack1.txt
  2. #cat/tmp/pack1.txt
  3. mariadb-server-5.5.60-1.el7_5.x86_64
  4. perl-DBI-1.627-4.el7.x86_64
  5. perl-DBD-MySQL-4.023-6.el7.x86_64
  6. perl-PlRPC-0.2020-14.el7.noarch
  7. perl-Net-Daemon-0.48-5.el7.noarch
  8. perl-IO-Compress-2.061-2.el7.noarch
  9. perl-Compress-Raw-Zlib-2.061-4.el7.x86_64
  10. mariadb-5.5.60-1.el7_5.x86_64
  11. perl-Data-Dumper-2.145-3.el7.x86_64
  12. perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64
  13. httpd-2.4.6-88.el7.centos.x86_64
  14. mailcap-2.1.41-2.el7.noarch
  15. httpd-tools-2.4.6-88.el7.centos.x86_64
  16. apr-util-1.5.2-6.el7.x86_64
  17. apr-1.4.8-3.el7_4.1.x86_64

Method one: how to use the cat command to install the packages listed in the file on Linux?

To achieve this goal, I will use the first method is simple and straightforward. To do this, create a file and add the list of packages you want to install.

For testing purposes, we will add only the following three packages to the file name.

  1. #cat/tmp/pack1.txt
  2. apache2
  3. mariadb-server
  4. nano

Simply run apt command can be one-time installation of all packages on Ubuntu / Debian system.

  1. # apt -y install $(cat/tmp/pack1.txt)
  2. Readingpackage lists...Done
  3. Building dependency tree
  4. Reading state information...Done
  5. The following packages were automatically installed and are no longer required:
  6. libopts25 sntp
  7. Use'sudo apt autoremove' to remove them.
  8. Suggested packages:
  9. apache2-doc apache2-suexec-pristine | apache2-suexec-custom spell
  10. The following NEW packages will be installed:
  11. apache2 mariadb-server nano
  12. 0 upgraded,3 newly installed,0 to remove and24not upgraded.
  13. Need to get339 kB of archives.
  14. Afterthis operation,1,377 kB of additional disk space will be used.
  15. Get:1 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2 amd64 2.4.29-1ubuntu4.6 [95.1 kB]
  16. Get:2 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 nano amd64 2.9.3-2 [231 kB]
  17. 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]
  18. Fetched339 kB in19s(18.0 kB/s)
  19. Selecting previously unselected package apache2.
  20. (Reading database ...290926 files and directories currently installed.)
  21. Preparing to unpack .../apache2_2.4.29-1ubuntu4.6_amd64.deb ...
  22. Unpacking apache2 (2.4.29-1ubuntu4.6)...
  23. Selecting previously unselected packagenano.
  24. Preparing to unpack .../nano_2.9.3-2_amd64.deb ...
  25. Unpackingnano(2.9.3-2)...
  26. Selecting previously unselected package mariadb-server.
  27. Preparing to unpack .../mariadb-server_1%3a10.1.38-0ubuntu0.18.04.1_all.deb ...
  28. Unpacking mariadb-server (1:10.1.38-0ubuntu0.18.04.1)...
  29. Processing triggers for ufw (0.36-0ubuntu0.18.04.1)...
  30. Setting up apache2 (2.4.29-1ubuntu4.6)...
  31. Processing triggers for ureadahead (0.100.0-20)...
  32. Processing triggers for install-info(6.5.0.dfsg.1-2)...
  33. Setting up nano(2.9.3-2)...
  34. update-alternatives:using/bin/nano to provide /usr/bin/editor (editor)inauto mode
  35. update-alternatives:using/bin/nano to provide /usr/bin/pico (pico)inauto mode
  36. Processing triggers forsystemd(237-3ubuntu10.20)...
  37. Processing triggers forman-db (2.8.3-2ubuntu0.1)...
  38. Setting up mariadb-server (1:10.1.38-0ubuntu0.18.04.1)...

As to delete, you use the same command format and the appropriate option.

  1. # apt -y remove $(cat/tmp/pack1.txt)
  2. Readingpackage lists...Done
  3. Building dependency tree
  4. Reading state information...Done
  5. The following packages were automatically installed and are no longer required:
  6. 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
  7. 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
  8. Use'apt autoremove' to remove them.
  9. The following packages will be REMOVED:
  10. apache2 mariadb-server nano
  11. 0 upgraded,0 newly installed,3 to remove and24not upgraded.
  12. Afterthis operation,1,377 kB disk space will be freed.
  13. (Reading database ...291046 files and directories currently installed.)
  14. Removing apache2 (2.4.29-1ubuntu4.6)...
  15. Removing mariadb-server (1:10.1.38-0ubuntu0.18.04.1)...
  16. Removingnano(2.9.3-2)...
  17. update-alternatives:using/usr/bin/vim.tiny to provide /usr/bin/editor (editor)inauto mode
  18. Processing triggers for ufw (0.36-0ubuntu0.18.04.1)...
  19. Processing triggers for install-info(6.5.0.dfsg.1-2)...
  20. Processing triggers forman-db (2.8.3-2ubuntu0.1)...
  1. #yum-y install $(cat/tmp/pack1.txt)

Use the command to uninstall the packages listed in the file based on RHEL (such as Centos, RHEL (Redhat) and OEL (Oracle Enterprise Linux)) system.

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

Use the following command to uninstall the packages listed in the file on the Fedora system.

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

Use the following command to uninstall the packages listed in the file from the openSUSE system.

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

Use the following pacman command installation packages listed in the file system based on Arch Linux (such as Manjaro and Antergos) of.

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

Use the following command to uninstall the packages listed in the file from the system based on Arch Linux (such as Manjaro and Antergos) in.

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

Method Two: How to use cat and xargs command to install the packages listed in the file in Linux.

Even, I prefer to use this method, because this is a very simple and direct way.

Use the following aptcommand to install package files listed on Debian-based systems (such as Debian, Ubuntu and Linux Mint).

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

Use the following aptcommand to uninstall the packages listed in the file from the Debian system (such as Debian, Ubuntu and Linux Mint) based.

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

Use the following yumcommand to install the packages listed in the file based on RHEL (such as Centos, RHEL (Redhat) and OEL (Oracle Enterprise Linux)) system.

  1. #cat/tmp/pack1.txt |xargsyum-y install

Use the command to uninstall the packages listed in the file from the system based on RHEL (such as Centos, RHEL (Redhat) and OEL (Oracle Enterprise Linux)) is.

  1. #cat/tmp/pack1.txt |xargsyum-y remove

Use the following dnfcommand to install the packages listed in the file on the Fedora system.

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

Use the following command to uninstall the packages listed in the file from the Fedora system.

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

Use the following zyppercommand to install package files listed on the openSUSE system.

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

Use the following command to uninstall the packages listed in the file from the openSUSE system.

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

Use the following pacmancommand to install the packages listed in the file system based on Arch Linux (such as Manjaro and Antergos) of.

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

Under uninstall packages using the command listed in the file from the system based on Arch Linux (such as Manjaro and Antergos) of.

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

Method three: how to use the package files listed in the For loop installed on Linux

We can also use the forloop command for this purpose.

Mounting a bulk package can use the following forcycle command.

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

To use the shell script to install the bulk package, please use the following forcycle.

  1. #vi/opt/scripts/bulk-package-install.sh
  2. #!/bin/bash
  3. for pack in`cat /tmp/pack1.txt`
  4. do apt -y remove $pack
  5. done

To bulk-package-install.shset the executable permission.

  1. #chmod+ bulk-package-install.sh

Finally, run this script.

  1. # sh bulk-package-install.sh

Four: how to list the files in the package While circulating installed on Linux

We can also use whileto achieve the purpose of the command loop.

Mounting a bulk package can use the following whilecycle command.

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

To use the shell script to install the bulk package, please use the following whilecycle.

  1. #vi/opt/scripts/bulk-package-install.sh
  2. #!/bin/bash
  3. file="/tmp/pack1.txt"
  4. while read -r pack
  5. do apt -y remove $pack
  6. done<"$file"

To bulk-package-install.shset the executable permission.

  1. #chmod+ bulk-package-install.sh

Finally, run this script.

  1. # sh bulk-package-install.sh

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

Author: Magesh Maruthamuthu topics: lujun9972 Translator: Way-WW proofread: wxy

This article from the LCTT original compiler, Linux China is proud

Guess you like

Origin www.linuxidc.com/Linux/2019-06/158988.htm
Recommended