Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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: askubuntu.com/questions/28644/how-can-i-backup-my-ppas –  Glutanimate Aug 31 '13 at 11:03
2  
Actually a superset. My question was about a script that generates the restore script, not just backup –  stwissel Sep 1 '13 at 12:19

3 Answers 3

up vote 29 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 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.

share|improve this answer
1  
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
1  
Note that PPA names may contain dots, so I think you want to change your regexp to http://ppa.launchpad.net/[a-z0-9-]\+/[a-z0-9.-]\+ –  kynan Jan 7 '13 at 10:28

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

You can show everything with:

grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*
share|improve this answer
1  
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
1  
I use grep ^[^#] ... -- It auto-hides all commented-out sources –  Ross Aiken Oct 30 '13 at 22:36

Your Answer

 
discard

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.