Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Solved] Is there some way to feed the contents of a text file into "apt-get install"?

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
TryingOutDebian
Posts: 12
Joined: 2023-11-24 16:38
Has thanked: 26 times

[Solved] Is there some way to feed the contents of a text file into "apt-get install"?

#1 Post by TryingOutDebian »

I have a simple txt file that is just a plain list of all the Debian packages which I've installed manually, mainly to make it easier for me to rebuild my system if I should lose it somehow. I'd like to be able to install all the packages on that list with a simple command. Now, the easy way to do that is to open the text file, copy the entire content, then type "sudo apt-get install" in a terminal and paste what I just copied afterwards. And that is how I've done it so far. But that means using a really long command. So I'm wondering if there could be a shorter command to do that.

I've tried

sudo cat NameOfTheFile.txt | apt-get install

but that doesn't work. That is, I always get told that there are zero packages to install, even if I intentionally removed one of the packages in order to test the thing.

Any ideas?
Last edited by TryingOutDebian on 2023-11-28 15:52, edited 1 time in total.
Just for reference, right now, I'm using the Mate version of Debian Bookworm.

User avatar
sunrat
Administrator
Administrator
Posts: 6512
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 119 times
Been thanked: 489 times

Re: [Installation] Is there some way to feed the contents of a text file into "apt-get install"?

#2 Post by sunrat »

Try

Code: Select all

xargs sudo apt -y install < NameOfTheFile.txt
Note: edited to add xargs which is needed to read the file from standard input.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
None1975
df -h | participant
df -h | participant
Posts: 1412
Joined: 2015-11-29 18:23
Location: Russia, Kaliningrad
Has thanked: 46 times
Been thanked: 70 times

Re: [Installation] Is there some way to feed the contents of a text file into "apt-get install"?

#3 Post by None1975 »

Hello.

For this purpose you can try make script.

For example:

Code: Select all

#!/bin/bash

# Install packages after installing base Debian with no GUI

# xorg display server installation and wm
sudo apt install -y xorg xterm

#Spectrwm
sudo apt install -y spectrwm sxhkd

#Other stuff
sudo apt install -y mate-terminal stterm xautolock dunst libnotify-bin sxiv


# Microcode for Intel/AMD
sudo apt install -y firmware-linux-nonfree firmware-realtek

# File Manager (eg. pcmanfm,krusader)
sudo apt install -y micro emacs w3m mc  ranger gvfs gvfs-backends thunar

# Network File Tools/System Events
sudo apt install -y dialog mtools dosfstools avahi-daemon

sudo systemctl enable avahi-daemon

# Neofetch/HTOP
sudo apt install -y neofetch htop lm-sensors smartmontools

# Various utilities
sudo apt install -y imagemagick scrot i3lock-fancy breeze-gtk-theme gnome-icon-theme rar unrar p7zip-full p7zip-rar zip unzip lxappearance

# Printing and bluetooth (if needed)
sudo apt install -y cups
sudo systemctl enable cups

# Browser and Mail client Installation (eg. chromium)
sudo apt install -y firefox-esr firefox-esr-l10n-ru thunderbird thunderbird-l10n-ru

# Aditional packages
sudo apt install -y zathura zathura-djvu  simple-scan preload

# Install fonts
sudo apt install -y fonts-font-awesome fonts-ubuntu fonts-liberation2 fonts-liberation xfonts-terminus fonts-jetbrains-mono

#Music and video packages
sudo apt install -y mpv sox libsox-fmt-all alsa-utils pulseaudio pavucontrol cmus weechat

#Torrents
sudo apt install -y qbittorrent aria2 keepassxc seahorse gpa mat2

#Install Office packages
sudo apt install -y libreoffice libreoffice-gtk3 libreoffice-l10n-ru gimp

#Network
sudo apt install -y network-manager

#Nvidia
sudo apt install -y linux-headers-amd64 

sudo apt install -y nvidia-driver

sudo apt autoremove

printf "\e[1;32mYou can now reboot! Thanks you.\e[0m\n"
Just don't forget to make this script executable with command

Code: Select all

chmod +x 
P.S.

You can name this file for example install.sh
OS: Debian 12.4 Bookworm / DE: Enlightenment
Debian Wiki | DontBreakDebian, My config files on github

User avatar
fabien
Forum Helper
Forum Helper
Posts: 689
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 62 times
Been thanked: 161 times

Re: [Installation] Is there some way to feed the contents of a text file into "apt-get install"?

#4 Post by fabien »

Code: Select all

$> cat /tmp/pkglist
isc-dhcp-client hello
dhcpcd-base
You can do

Code: Select all

#> apt-get -sV install $(cat /tmp/pkglist)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
dhcpcd-base is already the newest version (9.4.1-24~deb12u2).
Suggested packages:
   resolvconf (1.91+nmu1)
   avahi-autoipd (0.8-10)
   isc-dhcp-client-ddns (4.4.3-P1-2)
The following NEW packages will be installed:
   hello (2.10-3)
   isc-dhcp-client (4.4.3-P1-2)
   isc-dhcp-common (4.4.3-P1-2)
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Inst isc-dhcp-client (4.4.3-P1-2 Debian:12.2/stable [amd64])
Inst isc-dhcp-common (4.4.3-P1-2 Debian:12.2/stable [amd64])
Inst hello (2.10-3 Debian:12.2/stable [amd64])
Conf isc-dhcp-client (4.4.3-P1-2 Debian:12.2/stable [amd64])
Conf isc-dhcp-common (4.4.3-P1-2 Debian:12.2/stable [amd64])
Conf hello (2.10-3 Debian:12.2/stable [amd64])
The problem is that the arguments are not protected. This may seem almost theoretical or anecdotal in your case, but still. For exemple, if you use '*' in your package list as regular expression (for instance isc-dhcp.*) it can be subject to pathname expansion (glob).
A safer way is to populate an array. Arrays can be protected.

Code: Select all

#> declare -a MYPKGLIST    ### declare that MYPKGLIST is an array (optional, bash sets this attribute automatically when a variable is used as an array)

#> read -d '' -ra MYPKGLIST < <(cat /tmp/pkglist)     ### populates the array. The list in /tmp/pkglist can be space separated or new line separated, or both

#> declare -p MYPKGLIST   ### if you want to see the content of the array
declare -a MYPKGLIST=([0]="isc-dhcp-client" [1]="hello" [2]="dhcpcd-base")

#> apt-get -sV install "${MYPKGLIST[@]}"     ### the arguments are protected
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
dhcpcd-base is already the newest version (9.4.1-24~deb12u2).
Suggested packages:
   resolvconf (1.91+nmu1)
   avahi-autoipd (0.8-10)
   isc-dhcp-client-ddns (4.4.3-P1-2)
The following NEW packages will be installed:
   hello (2.10-3)
   isc-dhcp-client (4.4.3-P1-2)
   isc-dhcp-common (4.4.3-P1-2)
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Inst isc-dhcp-client (4.4.3-P1-2 Debian:12.2/stable [amd64])
Inst isc-dhcp-common (4.4.3-P1-2 Debian:12.2/stable [amd64])
Inst hello (2.10-3 Debian:12.2/stable [amd64])
Conf isc-dhcp-client (4.4.3-P1-2 Debian:12.2/stable [amd64])
Conf isc-dhcp-common (4.4.3-P1-2 Debian:12.2/stable [amd64])
Conf hello (2.10-3 Debian:12.2/stable [amd64])

TryingOutDebian
Posts: 12
Joined: 2023-11-24 16:38
Has thanked: 26 times

Re: [Installation] Is there some way to feed the contents of a text file into "apt-get install"?

#5 Post by TryingOutDebian »

Thank you, everyone! Especially fabien- your

#> apt-get -sV install $(cat /tmp/pkglist)

is the one that works best for me for this. There's no * in the list file, so no need to worry about that.
Just for reference, right now, I'm using the Mate version of Debian Bookworm.

CwF
Global Moderator
Global Moderator
Posts: 2720
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 41 times
Been thanked: 201 times

Re: [Solved] Is there some way to feed the contents of a text file into "apt-get install"?

#6 Post by CwF »

I'm wondering if the package 'dselect' would be useful for this?

User avatar
sunrat
Administrator
Administrator
Posts: 6512
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 119 times
Been thanked: 489 times

Re: [Solved] Is there some way to feed the contents of a text file into "apt-get install"?

#7 Post by sunrat »

Thanks to @fabien who actually tested my suggestion above. The command requires xargs to read the file from standard input. I tested it this time and it works.

Code: Select all

xargs sudo apt -y install < NameOfTheFile.txt
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

ab1jx
Posts: 111
Joined: 2016-01-23 21:28
Location: Heath, MA, USA
Has thanked: 2 times
Contact:

Re: [Solved] Is there some way to feed the contents of a text file into "apt-get install"?

#8 Post by ab1jx »

I did something like this once, take a look at the --recursive option to dpkg. You can do dpkg -ir with a list and it handles it quite well. You can reconstruct a machine or copy one, even email a list to someone remote.

User avatar
cds60601
df -h | participant
df -h | participant
Posts: 740
Joined: 2017-11-25 05:58
Location: Florida
Has thanked: 138 times
Been thanked: 66 times

Re: [Solved] Is there some way to feed the contents of a text file into "apt-get install"?

#9 Post by cds60601 »

If you are adventurous and using either testing or sid, there is apt-clone
Supercalifragilisticexpialidocious

jmgibson1981
Posts: 306
Joined: 2015-06-07 14:38
Has thanked: 11 times
Been thanked: 34 times

Re: [Solved] Is there some way to feed the contents of a text file into "apt-get install"?

#10 Post by jmgibson1981 »

CwF wrote: 2023-11-28 16:22 I'm wondering if the package 'dselect' would be useful for this?
I've tried numerous times to use the dselect method to restore packages on a fresh installation. Either I got it wrong every single time or the instructions I found are not 100% accurate. I haven't gotten it to work once. I end up processing the file to remove all but the package names then do the

Code: Select all

apt install $(cat /file)
method.

Post Reply