Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a text file with lots of package names.


# comment
# installing package3 because it was needed for...
package 3

package 4

How can I mass install all packages inside the text file without removing the comments?

share|improve this question
up vote 22 down vote accepted

Something along these lines ought to do the trick.

apt-get install $(grep -vE "^\s*#" filename  | tr "\n" " ")

The $(something) construction runs the something command, inserting its output in the command line.

The grep command will exclude any line beginning with a #, optionally allowing for whitespace before it. Then the tr command replaces newlines with spaces.

share|improve this answer
Very useful, thank you. – mikewhatever Dec 5 '13 at 16:01
Very slick. Great job. – Cerin Jul 1 '14 at 0:05
tr will fail with multi-byte end-of-line sequences (think \r\n), why don't you use xargs? – Ivan Anishchuk Feb 17 at 16:20

The following command is a (slight) improvement over the alternative because sudo apt-get install is not executed when the package list is empty.

xargs -a <(awk '/^\s*[^#]/' "$packagelist") -r -- sudo apt-get install

Note that the -a option reads items directly from a file instead of standard input. We don't want to pipe a file into xargs because stdin must remain unchanged for use by apt-get.

share|improve this answer
xargs is the right way to do this. Trick with <() is neat. – Ivan Anishchuk Feb 17 at 16:19
That's indeed the better solution because xargs makes sure ARG_MAX is not reached. – phk Jun 12 at 11:45
It has to be xargs -a $(awk '/^\s*[^#]/' "$packagelist") -r -- sudo apt-get -y install, not "<(" but "$(" and the option -y for apt-get would be a good idea. – thomas Jul 11 at 20:19
Process substitution redirects the output of awk into a file descriptor for xargs -a to read from. So you definitely want <( and not $(. Just try it and you'll see what I mean. If the command is to be running unattended and you already know exactly what's going to be installed then sure, they -y flag is a good idea. – Six Jul 12 at 3:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.