1

I have some package installed on a computer. I want to install similar packages for other computer.

I can list down all recent packages with this command

cat /var/log/dpkg.log | grep "\ install\ "

It will output something like

2015-02-18 19:33:46 install login:amd64 <none> 1:4.1.5.1-1ubuntu9
2015-02-18 19:33:46 install lsb-base:all <none> 4.1+Debian11ubuntu6
2015-02-18 19:33:46 install makedev:all <none> 2.3.1-93ubuntu1
2015-02-18 19:33:46 install module-init-tools:all <none> 15-0ubuntu6
2015-02-18 19:33:46 install mount:amd64 <none> 2.20.1-5.1ubuntu20
2015-02-18 19:33:46 install mountall:amd64 <none> 2.53

This list is quite big.

I want to make it something like

sudo apt-get install login lsb-base module-init-tools mount mountall
  • You need to cut that list to get only the name of the packages, and then pass those names into the apt-get install command – LnxSlck Jun 23 '15 at 08:44
  • Yes, i am trying to figure out a way to do that. I think awk command can do it but not sure about it. Can you direct me towards a way to do it ? – saurabh agarwal Jun 23 '15 at 08:48
  • cat /var/log/apt/history.log | grep "\ install\ " – LnxSlck Jun 23 '15 at 09:02
  • Take a look at Alvin Row answer here http://askubuntu.com/questions/17012/is-it-possible-to-get-a-list-of-most-recently-installed-packages – LnxSlck Jun 23 '15 at 09:02
  • thanks, but again it gives me output in several line. problem is I have 1000s of such line. I want to extract all those packages name. – saurabh agarwal Jun 23 '15 at 09:10
  • You will find what you're looking for in the link that i told you. – LnxSlck Jun 23 '15 at 09:25

3 Answers3

2

You don't need to make it as you wish, there is a better way of backing up a list of programs:

On the First compute run those commands:

dpkg --get-selections > /some-path/packages.list

sudo cp -R /etc/apt/sources.list* /some-path/

sudo apt-key exportall > /some-path/Repo.keys

Then copy those files to the other computer and there run those commands to install exactly the same apps from the first computer:

sudo apt-key add /some-path/Repo.keys

sudo cp -R /some-path/sources.list* /etc/apt/

sudo apt-get update

sudo apt-get install dselect

sudo dpkg --set-selections < /some-path/packages.list

sudo apt-get dselect-upgrade -y
Maythux
  • 84,289
  • dpkg --get-selections will list all the packages in any conditions....this will show the packages that were installed automatically, manually, comes preinstalled..OP wants only the manually installed ones.. – heemayl Jun 23 '15 at 10:21
  • @heemayl yes you alright list all the packages in any conditions but I didn't see anywhere the OP asks for manually installed packages. He asked: I have some package installed on a computer. I want to install similar packages for other computer. He didn't say I installed some packages – Maythux Jun 23 '15 at 10:25
  • OP is parsing /var/log/dpkg.log..that is the idea of it all in my opinion.. – heemayl Jun 23 '15 at 10:26
  • Mmmm Maybe you are right, wait for the OP response – Maythux Jun 23 '15 at 10:27
  • @Maythux Thanks , I only needed manually installed packages but as both computers are freshly installed. I need all the packages on computer1 to be installed on computer2. – saurabh agarwal Jun 23 '15 at 11:39
  • Then I think also my answer would be applicable – Maythux Jun 23 '15 at 12:31
  • Yes, it is applicable. – saurabh agarwal Jun 24 '15 at 03:53
  • I prefer this answer as it won't include packages that were installed then removed. – LovesTha Nov 29 '16 at 00:22
1

command to do this is

cat /var/log/apt/history.log | grep "\ install\ " | awk '/ install / {printf "%s ",$4 }'
1

Save all the installed packages shown by /var/log/dpkg.log in an array:

mapfile -t packages < <(grep -Po '.* install \K[^ ]+' /var/log/dpkg.log)

Here the array packages will contain all the package names.

Now you can simple do:

echo "${packages[@]}"

to see the package names in a space separated form.

This will work well with apt-get command:

sudo apt-get install --dry-run "${packages[@]}"

The above command will be expanded to:

sudo apt-get install --dry-run libntlm0:amd64 libgsasl7:amd64 ....

If you have ssh access from the new computer to the one where the package are installed, from NEW computer you can use:

$ mapfile -t packages < <(ssh OLD 'grep -Po ".* install \K[^ ]+" /var/log/dpkg.log'))"

This will save the package names from OLD computer in the array packages. change the ssh parameters accordingly.

Now you can simply do:

$ sudo apt-get install "${packages[@]}"
heemayl
  • 91,753