Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipes with Apt Package Manager

I have two files, the first one called packages.txt which is list of packages:

gcc
emacs
vim
python
...

Now, when I run the command

cat packages.txt | tr '\n' ' ' | apt-get install

This basically converts the file into one line of packages separated by space. It does not install all the packages in packages.txt. (I think it only installs the first one) Does anyone knows why?

like image 655
Husain Avatar asked Sep 17 '25 18:09

Husain


2 Answers

Try using xargs:

xargs -d '\n' -- apt-get install < packages.txt

Or

xargs -d '\n' -n 1 -- apt-get install < packages.txt

Make sure packages.txt is not in DOS format:

sed -i 's|\r||' packages.txt
like image 114
konsolebox Avatar answered Sep 20 '25 08:09

konsolebox


The pipe operator sends the stdout of one application into the next (as stdin. Unfortunately, apt-get does not read from stdin. Rather, apt-get takes a list of packages on the command line. I think what you are attempting to do is more along the lines of

apt-get install $(cat packages.txt | tr '\n' ' ')

or

apt-get install `cat packages.txt | tr '\n' ' '`

Which is to say evaluate the file list, and pass it in as an arguments to a single apt-get call.

like image 41
Jefferey Cave Avatar answered Sep 20 '25 10:09

Jefferey Cave