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


217

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

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

我知道我可以研究/etc/apt/sources.listand /etc/apt/sources.list.d,但是我正在寻找一种生成脚本的方法,该脚本可以apt-add-repository在新系统上执行所有命令(可以获取所有键)。

有任何想法吗?

Answers:


106

您可以通过以下方式显示所有内容:

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

13
怎么样egrep -v '^#|^ *$' /etc/apt/sources.list /etc/apt/sources.list.d/*去除注释掉线和空白行?

3
你能解释一下使用^grepgrep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*

4
@ vasa1脱字号^和美元符号$是元字符,它们分别与行的开头和结尾处的空字符串匹配。
wojox 2012年

4
我使用grep ^ [^#] ...-它会自动隐藏所有已注释掉的资​​源
Ross Aiken

12
如果您不打算过滤掉任何内容,那么运行起来会不会更简单cat /etc/apt/sources.list /etc/apt/sources.list.d/*
jbo5112

99

感谢您的指导。经过一点清理,我得到了一个脚本,其中列出了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

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


1
在您的grep -o示例中,\` in [a-z0-9\-]并没有达到您的期望。它实际上与文字反斜杠匹配。你并不需要逃避-时候它是在开始或结束[]列表; 实际上,您无法逃脱它!..在这种情况下,\`(可能)不会引起问题,因为您(希望)在条目中不会遇到反斜线deb
Peter.O 2012年

2
请注意,PPA名称可能包含点,因此我认为您要将正则表达式更改为http://ppa.launchpad.net/[a-z0-9-]\+/[a-z0-9.-]\+
kynan

不,您想将正则表达式更改为[[:graph:]] [a-z...blah.anything]因为它将与任何字母数字+标点符号匹配-这就是PPA名称的组成。
MichalH 2015年

我想您应该deb在每个存储库行的开头都包含单词(如果未以ppa:$USER/$PPA形式给出)。
jarno

@stwissel您使用过find和grep的任何特定原因?您可以轻松地执行shell解析的glob,并将其传递给grep。grep -Po "(?<=^deb\s).*?(?=#|$)" /etc/apt/{sources.list,sources.list.d/*.list} | while read ENTRY ; do echo $ENTRY; done请注意,按照本文的说明,您会看到每个条目的文件名,因此您需要从结果开头到第一个冒号进行修整,但是使用cut并不是很困难。uniq如果您不希望同一来源的多个条目(例如,如果您安装了Google Chrome Stable / Beta / Dev),则可能还需要传递它。
dragon788

23

让我感到惊讶的是,尚未发布将所有启用的二进制软件源以及在其中指定的文件一起获得的最简单但最有效的方法:

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

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

它实际上仅搜索*.list将由解析的所有文件,apt例如,不搜索*.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

1
按照公认的答案,OP似乎希望以ppa:<user>/<project>表格形式显示PPA 。
muru

该问题实际上要求生成一个可安装/启用所有存储库的脚本。但问题标题仅与列出它们有关。同样,得分第二高的答案也只列出了它们,但是列出的太多了。
字节指挥官

很好,但是我已经投票了。:D
大师

您可以对grep使用`-h`选项来省略文件名。
jarno

11

运行以下命令:

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

资源


在仿生中,这会打印诸如' mirrors.nic.funet.fi/ubuntubionic-security/main '之类的行
jarno

1
注意:apt-cache policy仅在运行后显示回购apt-get update。如果您只是添加了一个add-apt-repositoryapt-cache policyapt-get update
仓库

每个@wisbucky: sudo apt update > /dev/null 2>&1 && sudo apt-cache policy | grep http | awk '{print $2 $3}' | sort -u效果很好。 gist.github.com/bmatthewshea/229da822f1f02157bff192a2e4a8ffd1
bshea

4

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

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以便仅获取当前启用的软件源。


Thx的反馈。cat会按原样列出文件,因此我需要手动对其进行编辑以生成脚本(如问题中所述)。存储库的挑战:如果仅从/ etc / apt复制文件,则不会获得存储库密钥。这就是为什么我想要一个脚本为我们获取它们的原因
stwissel 2012年

2

因此,我们进行了一些挖掘AptPkg::Class

所以使用perl我们可以做一些简单的事情。

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

这为我们提供了所有AptPkg::Class::PkgFile软件包的列表。您可能会以此生成apt-add-repository命令。


2

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

2

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

相关的部分是其中的5行list_sourceslist_ppa功能,其余只是将其包装在方便的shell脚本中的样板。

list-apt-repositories

#!/bin/sh

usage () {
  cat >&2 <<USAGE
$0 [--ppa-only]

Options:
  --ppa-only            only list PPAs
USAGE
  exit $1
}

list_sources () {
  grep -E '^deb\s' /etc/apt/sources.list /etc/apt/sources.list.d/*.list |\
    cut -f2- -d: |\
    cut -f2 -d' ' |\
    sed -re 's#http://ppa\.launchpad\.net/([^/]+)/([^/]+)(.*?)$#ppa:\1/\2#g'
}

list_ppa () {
  list_sources | grep '^ppa:'
}

generate=list_sources

while test -n "$1"
do
  case "$1" in
    -h|--help) usage 1;;
    --ppa-only) generate=list_ppa;;
    *)
      printf -- "Unknown argument '$1'\n" >&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 <<USAGE
Usage: $0 < PATH_TO_LIST_OF_REPOS
       list-apt-repositories [--ppa-only] | $0

No options recognized.

Reads list of repositories from stdin and generates a script to install them
using \`add-apt-repository(1)\`. The script is printed to stdout.

The generated script supports an optional
\`-y\` or \`--yes\` argument which causes the \`add-apt-repository\` commands
to be run with the \`--yes\` flag.
USAGE
  exit 1
fi

cat <<INSTALL_SCRIPT
#!/bin/sh
y=
case "\$1" in
  -y|--yes) y=\$1;;
  '') y=;;
  *)
    printf '%s\n' "Unknown option '\$1'" "Usage: \$0 [{-y|--yes}]" >&2
    exit 1
  ;;
esac
INSTALL_SCRIPT

xargs -d'\n' printf "add-apt-repository \$y '%s'\n"

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


1

要添加它,请将ppa.launchpad.net行添加为ppa:$ USER / $ PPA。从* .list文件中以全行添加其他存储库。没有欺骗行。

#!/ bin / bash
#我的〜/ bin / mk_repositories_restore_script
mkdir -p〜/ bin 
x =〜/ bin / restore_repositories
回声\#\!/ bin / bash> $ x
chmod u + x $ x
(
 在$()中查找APT(找到/ etc / apt / -name \ *。list)
    做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
 完成
)| 排序| uniq | tee -a〜/ bin / restore_repositories

0

谢谢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

0
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)。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.