如何从命令行获取所有存储库和PPA的列表到安装脚本?

我知道如何列出我系统上安装的所有软件包 。

但是,如何才能将所有存储库和PPA的列表放入一个脚本中,我可以在新机器上运行以复制包括密钥的存储库设置?

我知道我可以查看/etc/apt/sources.list/etc/apt/sources.list.d ,但我正在寻找一种方法来生成一个脚本,该脚本在新的上执行所有apt-add-repository命令系统(排除获取所有密钥)。

有任何想法吗?

谢谢你的指点。 通过一点清理,我得到了一个列出PPA的脚本,但没有列出任何其他存储库:

 #! /bin/sh # listppa Script to get all the PPA installed on a system ready to share for reininstall for APT in `find /etc/apt/ -name \*.list`; do grep -o "^deb http://ppa.launchpad.net/[a-z0-9\-]\+/[a-z0-9\-]\+" $APT | while read ENTRY ; do USER=`echo $ENTRY | cut -d/ -f4` PPA=`echo $ENTRY | cut -d/ -f5` echo sudo apt-add-repository ppa:$USER/$PPA done done 

当您使用listppa > installppa.sh调用它时,您将获得一个脚本,您可以在新计算机上复制该脚本以重新安装所有PPA。

下一站:为其他存储库执行此操作:

 #! /bin/sh # Script to get all the PPA installed on a system for APT in `find /etc/apt/ -name \*.list`; do grep -Po "(?<=^deb\s).*?(?=#|$)" $APT | while read ENTRY ; do HOST=`echo $ENTRY | cut -d/ -f3` USER=`echo $ENTRY | cut -d/ -f4` PPA=`echo $ENTRY | cut -d/ -f5` #echo sudo apt-add-repository ppa:$USER/$PPA if [ "ppa.launchpad.net" = "$HOST" ]; then echo sudo apt-add-repository ppa:$USER/$PPA else echo sudo apt-add-repository \'${ENTRY}\' fi done done 

这应该可以解决问题。 我需要一个超级用户的问题来找出正确的正则表达式。

你可以用以下内容展示:

 grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* 

令我感到惊讶的是,将所有已启用的二进制软件源与它们指定的文件一起使用的最简单但最有效的方法尚未发布:

 grep -r --include '*.list' '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/ 

从所有处理过的文件中,这将打印以deb开头的每一行。 这不包括注释行和deb-src行以启用源代码存储库。

它实际上只搜索将由apt解析的所有*.list文件,但是例如没有*.list.save文件用于备份或其他具有非法名称的文件。


如果你想要更短但可能只有99.9%的情况下正确输出可能搜索太多文件(包括所有/etc/apt/sources.list*文件和目录,不仅仅是/etc/apt/sources.list和` /etc/apt/sources.list.d/*),你也可以使用这个:

 grep -r --include '*.list' '^deb ' /etc/apt/sources.list* 

除非有文件不应该存在,否则输出将是相同的。


我的机器上的示例输出将是:

 /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily main restricted /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-updates main restricted /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily universe /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-updates universe /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily multiverse /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-updates multiverse /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-backports main restricted universe multiverse /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-security main restricted /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-security universe /etc/apt/sources.list:deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-security multiverse /etc/apt/sources.list:deb http://archive.canonical.com/ubuntu wily partner /etc/apt/sources.list.d/maarten-fonville-ubuntu-ppa-wily.list:deb http://ppa.launchpad.net/maarten-fonville/ppa/ubuntu wily main /etc/apt/sources.list.d/webupd8team-ubuntu-tor-browser-wily.list:deb http://ppa.launchpad.net/webupd8team/tor-browser/ubuntu wily main /etc/apt/sources.list.d/fossfreedom-ubuntu-indicator-sysmonitor-wily.list:deb http://ppa.launchpad.net/fossfreedom/indicator-sysmonitor/ubuntu wily main /etc/apt/sources.list.d/getdeb.list:deb http://archive.getdeb.net/ubuntu wily-getdeb apps 

如果你想要更漂亮的输出,让我们通过sed管道:

 grep -r --include '*.list' '^deb ' /etc/apt/ | sed -re 's/^\/etc\/apt\/sources\.list((\.d\/)?|(:)?)//' -e 's/(.*\.list):/\[\1\] /' -e 's/deb http:\/\/ppa.launchpad.net\/(.*?)\/ubuntu .*/ppa:\1/' 

我们会看到这个:

 deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily main restricted deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-updates main restricted deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily universe deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-updates universe deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily multiverse deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-updates multiverse deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-backports main restricted universe multiverse deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-security main restricted deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-security universe deb http://ftp-stud.hs-esslingen.de/ubuntu/ wily-security multiverse deb http://archive.canonical.com/ubuntu wily partner [maarten-fonville-ubuntu-ppa-wily.list] ppa:maarten-fonville/ppa [webupd8team-ubuntu-tor-browser-wily.list] ppa:webupd8team/tor-browser [fossfreedom-ubuntu-indicator-sysmonitor-wily.list] ppa:fossfreedom/indicator-sysmonitor [getdeb.list] deb http://archive.getdeb.net/ubuntu wily-getdeb apps 

运行以下命令:

 apt-cache policy | grep http | awk '{print $2 $3}' | sort -u 

资源

我使用此命令列出所有已配置的软件源(存储库), 包括当前已禁用的软件源:

 cat /etc/apt/sources.list; for X in /etc/apt/sources.list.d/*; do echo; echo; echo "** $X:"; echo; cat $X; done 

我主要用它来排除故障; 这当然可以合并到脚本中,但您可能希望将/etc/apt/sources.list.d/*缩小到/etc/apt/sources.list.d/*.list这样您才能获得当前启用的软件源。

所以,做一些挖掘,我们有AptPkg::Class

所以使用perl我们可以做这样简单的事情..

 perl -MAptPkg::Cache -MData::Dumper -E'say Dumper [AptPkg::Cache->new->files()]' | less 

这将获得所有AptPkg::Class::PkgFile包的列表。 您可以使用它生成apt-add-repository命令。

https://repogen.simplylinux.ch/将为您提供适用于您的Ubuntu版本的所有PPA的列表。 这是一个没有源文件的生成列表,没有三星打印机ppa:

 #------------------------------------------------------------------------------# # OFFICIAL UBUNTU REPOS # #------------------------------------------------------------------------------# ###### Ubuntu Main Repos deb http://us.archive.ubuntu.com/ubuntu/ yakkety main restricted universe multiverse ###### Ubuntu Update Repos deb http://us.archive.ubuntu.com/ubuntu/ yakkety-security main restricted universe multiverse deb http://us.archive.ubuntu.com/ubuntu/ yakkety-updates main restricted universe multiverse deb http://us.archive.ubuntu.com/ubuntu/ yakkety-proposed main restricted universe multiverse deb http://us.archive.ubuntu.com/ubuntu/ yakkety-backports main restricted universe multiverse ###### Ubuntu Partner Repo deb http://archive.canonical.com/ubuntu yakkety partner #------------------------------------------------------------------------------# # UNOFFICIAL UBUNTU REPOS # #------------------------------------------------------------------------------# ###### 3rd Party Binary Repos #### Flacon PPA - http://kde-apps.org/content/show.php?content=113388 ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F2A61FE5 deb http://ppa.launchpad.net/flacon/ppa/ubuntu yakkety main #### Gimp PPA - https://launchpad.net/~otto-kesselgulasch/+archive/gimp ## Run this command: sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 614C4B38 deb http://ppa.launchpad.net/otto-kesselgulasch/gimp/ubuntu yakkety main #### Google Chrome Browser - http://www.google.com/linuxrepositories/ ## Run this command: wget -q https://dl.google.com/linux/linux_signing_key.pub -O- | sudo apt-key add - deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main #### Google Earth - http://www.google.com/linuxrepositories/ ## Run this command: wget -q https://dl.google.com/linux/linux_signing_key.pub -O- | sudo apt-key add - deb [arch=amd64] http://dl.google.com/linux/earth/deb/ stable main #### Highly Explosive PPA - https://launchpad.net/~dhor/+archive/myway ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93330B78 deb http://ppa.launchpad.net/dhor/myway/ubuntu yakkety main #### JDownloader PPA - https://launchpad.net/~jd-team ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6A68F637 deb http://ppa.launchpad.net/jd-team/jdownloader/ubuntu yakkety main #### Lazarus - http://www.lazarus.freepascal.org/ ## Run this command: gpg --keyserver hkp://pgp.mit.edu:11371 --recv-keys 6A11800F && gpg --export --armor 0F7992B0 | sudo apt-key add - deb http://www.hu.freepascal.org/lazarus/ lazarus-stable universe #### LibreOffice PPA - http://www.documentfoundation.org/download/ ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1378B444 deb http://ppa.launchpad.net/libreoffice/ppa/ubuntu yakkety main #### MEGA Sync Client - https://mega.co.nz/ deb http://mega.nz/linux/MEGAsync/xUbuntu_16.10/ ./ #### MKVToolnix - http://www.bunkus.org/videotools/mkvtoolnix/ ## Run this command: wget -q http://www.bunkus.org/gpg-pub-moritzbunkus.txt -O- | sudo apt-key add - deb http://www.bunkus.org/ubuntu/yakkety/ ./ #### Mozilla Daily Build Team PPA - http://edge.launchpad.net/~ubuntu-mozilla-daily/+archive/ppa ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 247510BE deb http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu yakkety main #### muCommander - http://www.mucommander.com/ ## Run this command: sudo wget -O - http://apt.mucommander.com/apt.key | sudo apt-key add - deb http://apt.mucommander.com stable main non-free contrib #### Opera - http://www.opera.com/ ## Run this command: sudo wget -O - http://deb.opera.com/archive.key | sudo apt-key add - deb http://deb.opera.com/opera/ stable non-free #### Oracle Java (JDK) Installer PPA - http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886 deb http://ppa.launchpad.net/webupd8team/java/ubuntu yakkety main #### PlayDeb - http://www.playdeb.net/ ## Run this command: wget -O- http://archive.getdeb.net/getdeb-archive.key | sudo apt-key add - deb http://archive.getdeb.net/ubuntu yakkety-getdeb games #### SABnzbd PPA - http://sabnzbd.org/ ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4BB9F05F deb http://ppa.launchpad.net/jcfp/ppa/ubuntu yakkety main #### SimpleScreenRecorder PPA - http://www.maartenbaert.be/simplescreenrecorder/ ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 283EC8CD deb http://ppa.launchpad.net/maarten-baert/simplescreenrecorder/ubuntu yakkety main #### Steam for Linux - http://store.steampowered.com/about/ ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F24AEA9FB05498B7 deb [arch=i386] http://repo.steampowered.com/steam/ precise steam #### Syncthing - https://syncthing.net/ ## Run this command: curl -s https://syncthing.net/release-key.txt | sudo apt-key add - deb http://apt.syncthing.net/ syncthing release #### Tor: anonymity online - https://www.torproject.org ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 886DDD89 deb http://deb.torproject.org/torproject.org yakkety main #### Unsettings PPA - http://www.florian-diesch.de/software/unsettings/ ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0FEB6DD9 deb http://ppa.launchpad.net/diesch/testing/ubuntu yakkety main #### VirtualBox - http://www.virtualbox.org ## Run this command: wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox_2016.asc -O- | sudo apt-key add - deb http://download.virtualbox.org/virtualbox/debian yakkety contrib #### Webmin - http://www.webmin.com ## Run this command: wget http://www.webmin.com/jcameron-key.asc -O- | sudo apt-key add - deb http://download.webmin.com/download/repository sarge contrib #### WebUpd8 PPA - http://www.webupd8.org/ ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4C9D234C deb http://ppa.launchpad.net/nilarimogard/webupd8/ubuntu yakkety main #### Xorg Edgers PPA - https://launchpad.net/~xorg-edgers ## Run this command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8844C542 deb http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu yakkety main here is a generated list without source files and no samsung printer ppa #### Yuuguu - http://yuuguu.com deb http://update.yuuguu.com/repositories/apt hardy multiverse 

这是我的脚本“ list-apt-repositories ”,它列出了“ /etc/sources.list"和“ /etc/sources.list.d/*.list ”中的所有存储库。 您可以添加--ppa-only以仅显示PPA。 PPA自动转换为ppa:USER/REPO格式。

相关部分是list_sourceslist_ppa函数中的5行,其余部分只是将它包装在一个方便的shell脚本中的样板。

list-apt-repositories

 #!/bin/sh usage () { cat >&2 <&2 usage 2 ;; esac shift done $generate 

并制作一个安装脚本,管道到另一个脚本“ make-apt-repository-install-script ”。 生成的脚本支持非交互式使用的-y / --yes参数(请参阅add-apt-repository(1) )。

make-apt-repository-install-script

 #!/bin/sh if test -n "$1" then cat >&2 <&2 exit 1 ;; esac INSTALL_SCRIPT xargs -d'\n' printf "add-apt-repository \$y '%s'\n" 

同样,重要的部分是最后一行的xargs命令,其余部分是样板文件。

要将ppa.launchpad.net行添加为ppa:$ USER / $ PPA。 使用* .list文件中的完整行添加其他存储库。 没有欺骗线。

 #!/斌/庆典
 #My~ / bin / mk_repositories_restore_script
 mkdir -p~ / bin 
 X =〜/斌/ restore_repositories
 echo \#\!/ bin / bash> $ x
 chmod u + x $ x
 (
  for $ APT(查找/ etc / apt / -name \ *。列表)
    做sed -n -e'/ ^ deb / {
      /ppa\.launchpad/s/\(.*,/\/[^\/]*.\)\([^ \ t] * \)\(。* $ \)/ sudo apt-add-repository ppa :\ 2 / p;
      /ppa\.launchpad/!s / \(deb [\ t] * \)\(。* $ \)/ sudo apt-add-repository \ 2 / p;
     ''APT
  DONE
 )| 排序|  uniq |  tee -a~ / bin / restore_repositories

谢谢BobDodds!
如果有人有兴趣,我已经更新了你的代码(希望你不介意)..
此脚本将仅键入用户添加的PPA(/etc/apt/sources.list.d)。

  #!/bin/bash # My ~/bin/mk_repositories_restore_script mkdir -p ~/bin x=~/bin/restore_repositories echo \#\!/bin/bash > $x chmod u+x $x ( for APT in $( find /etc/apt/ -name \*.list ) do sed -n -e '/^deb /{ /ppa\.launchpad/s/\(.*\/\/[^\/]*.\)\([^ \t]*\)\(.*\/ubuntu.*$\)/ppa:\2/p; }' $APT done ) | sort | uniq | tee -a ~/bin/restore_repositories 
 sed -r -e '/^deb /!d' -e 's/^([^#]*).*/\1/' -e 's/deb http:\/\/ppa.launchpad.net\/(.+)\/ubuntu .*/ppa:\1/' -e "s/.*/sudo add-apt-repository '&'/" /etc/apt/sources.list /etc/apt/sources.list.d/* 

但是,这并不会生成启用可能的源存储库(deb-src)的命令。

安装ppa-purge

 apt install ppa-purge 

然后通过选项卡完成获得ppa列表…

ppa-purge -o (按Tab键两次)