How can I get a list of all repositories and PPAs from the command line into an install script?

You can show everything with:

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

Thanks for the pointers. With a little cleanup I got a script that lists the PPAs, but not any other repository:

#! /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

When you call it with listppa > installppa.sh you get a script you can copy on a new machine to reinstall all PPA.

Next stop: do that for the other repositories:

#! /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

This should do the trick. I needed a question on superuser to figure out the correct regex.


I am surprised that the simplest but most effective way to get all enabled binary software sources together with the file they're specified in hasn't been posted yet:

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

From all processed files, this will print every line starting with deb. This excludes commented lines as well as deb-src lines to enable source code repositories.

It searches really only all *.list files that will be parsed by apt, but e.g. no *.list.save files used for backup or others with illegal names.


If you want a shorter but possibly only in 99.9% of all cases correct output that may search too much files (includes all /etc/apt/sources.list* files and directories, not only /etc/apt/sources.list and `/etc/apt/sources.list.d/*), you could also use this:

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

Unless there are files that shouldn't be there, the output will be the same.


An example output on my machine would be this:

/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

If you want prettier output, let's pipe it through 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/'

And we will see this:

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

Run the following command:

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

Source


Here is my script, "list-apt-repositories", which lists all repositories in "/etc/sources.list" and "/etc/sources.list.d/*.list". You can add --ppa-only to show only the PPAs. PPAs are automatically transformed to ppa:USER/REPO format.

The relevant parts are the 5 lines in list_sources and list_ppa functions, the rest is just boilerplate to wrap it in a handy shell script.

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

And to make an install script, pipe into another script "make-apt-repository-install-script". The generated script supports the -y/--yes argument for non-interactive use (see 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"

Again, the important part is the xargs command on the last line, the rest is boilerplate.