Listing the packages on Debian/Ubuntu in one line

You can use dpkg to list all the packages that are selected on your Debian or Ubuntu system:

[sourcecode language=”plain”]# dpkg –get-selections[/sourcecode]

But today I needed a way to list only the installed packages in just one line, so I could copy their names and use apt-get to install the same packages on another system. So I used the following command:

[sourcecode language=”plain”]# dpkg –get-selections | grep "[ \t]*install$" | sed ‘s/[ \t]*install$//g’ | awk ‘BEGIN { packages = "" } { packages = packages " " $1 } END { print packages }’
acpi-support-base acpid adduser apt apt-utils […] long list of packages […] xsltproc xz-utils yelp zenity zlib1g zlib1g-dev[/sourcecode]

You can use the output above to easily install the same packages on another system using apt-get install <packages>.