Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I know how to list all packages installed on my system.

But how could I get a list of all repositories and PPA's?

I know I can look into /etc/apt/sources.list and /etc/apt/sources.list.d, but I'm looking for a way to generate a script that executes all apt-add-repository commands on a new system (that sorts out getting all keys).

Any ideas?

share|improve this question
Possible duplicate: –  Glutanimate Aug 31 '13 at 11:03
Actually a superset. My question was about a script that generates the restore script, not just backup –  stwissel Sep 1 '13 at 12:19

4 Answers 4

up vote 40 down vote accepted

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

When you call it with listppa > 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 [ "" = "$HOST" ]; then
            echo sudo apt-add-repository ppa:$USER/$PPA
            echo sudo apt-add-repository \'${ENTRY}\'

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

share|improve this answer
In your grep -o example, the \` in [a-z0-9\-] is not doing what you expect. It actually matches a literal backslash. You don't need to escape the - when it is at the start or end of the [] list; actually, you can't escape it!.. In this case the \` (probably) won't cause a problem, because you (hopefully) won't encounter a backslash in the deb entry. –  Peter.O Sep 7 '12 at 3:51
Note that PPA names may contain dots, so I think you want to change your regexp to[a-z0-9-]\+/[a-z0-9.-]\+ –  kynan Jan 7 '13 at 10:28
No, you want to change the regex to [[:graph:]] instead of [a-z...blah.anything] because that will match any alphanumeric + punctuation characters - that is what the PPA names consist of. –  Mike Aug 25 at 13:11

You can show everything with:

grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*
share|improve this answer
What about egrep -v '^#|^ *$' /etc/apt/sources.list /etc/apt/sources.list.d/* to remove lines commented out and blank lines? –  user25656 Jun 10 '12 at 13:22
could you please explain the use of ^ after grep in grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*? –  user25656 Jun 10 '12 at 13:25
@vasa1 The caret ^ and the dollar sign $ are metacharacters that respectively match the empty string at the beginning and end of a line. –  wojox Jun 11 '12 at 14:22
wojox, @vasa1: thx for the contribution. It helped to get clarity how to sort this out. –  stwissel Jun 12 '12 at 11:30
I use grep ^[^#] ... -- It auto-hides all commented-out sources –  Ross Aiken Oct 30 '13 at 22:36

I use this command to list all configured software sources (repositories), including currently disabled ones:

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

I use this primarily for troubleshooting; this can certainly be incorporated into scripts but you may want to narrow /etc/apt/sources.list.d/* to /etc/apt/sources.list.d/*.list so you only get currently enabled software sources.

share|improve this answer
Thx for the feedback. cat lists the files as they are, so I would need to manually edit it to generate a script (as stated in the question). The challenge with repositories: if you just copy the files from /etc/apt you don't get the repository keys. This is why I want a script that fetches them for us –  stwissel Jun 12 '12 at 11:29

To have it add lines as ppa:$USER/$PPA. Add other repositories with their full line from *.list files. No dupe lines.

# My ~/bin/mk_repositories_restore_script
mkdir -p ~/bin 
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]*\)\(.*$\)/sudo apt-add-repository ppa:\2/p;
     /ppa\.launchpad/!s/\(deb[ \t]*\)\(.*$\)/sudo apt-add-repository \2/p;
    }' $APT
) | sort | uniq | tee -a ~/bin/restore_repositories
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.