Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two repository that I want my script to verify if they are on the system. How can I verify by command line if a repository is already added to the system and, if not, add them?

share|improve this question
up vote 5 down vote accepted

You can see all repository enabled in terminal with this command below (It will print all repository line starting with deb or deb-src)

find /etc/apt/ -name *.list | xargs cat | grep  ^[[:space:]]*deb

Explanation: first find find all files with .list extension in /etc/apt directory and feeds xargs the list, which in turn print all text on all of those files to the display device i.e Monitor. Then grep select those lines of which starts with deb. I have used extra step to include those lines also which starts with whitespace characters instead of deb.

If you want to see only the repositories with binary packages not source packages (deb-src), use another grep to exclude entries with deb-src like this one:

find /etc/apt/ -name *.list | xargs cat | grep  ^[[:space:]]*deb | grep -v deb-src

To add the repository using command line, just use this kind of command:

(I am assuming your repository source line is like this deb precise main )

echo deb precise main | sudo tee -a /etc/apt/sources.list

This command uses tee program which is used to read and write from standard input to standard output. Here, it takes the echoed repo line and put that line at the end of /etc/apt/sources.list file which is the main repository source file.

Please note that without using -a option for tee the entire sources.list will be replaced with the just echoed line.

Alternatively you can use your own file for the custom repo without touching sources.list file. For example,

echo deb precise main | sudo tee -a /etc/apt/sources.list.d/teles.list

It will create a file in /etc/apt/sources.list.d directory with name teles.list containing line deb precise main.

Note that, to add repository source to the system your script need to be run with root priviledge

share|improve this answer

I dont know if this is what you need but typing


in a terminal or in dash-command windows will open a list of software sources.

Use this command to add your PPA

sudo add-apt-repository ppa:YOUR PPA HERE

Hope that helps

EDIT: You can edit the file with sudo nano /etc/apt/sources.list if that's needed. nano is a terminal text editor you may have to do this first

sudo apt-get install nano

to install it.

share|improve this answer
Isn't there a command line that make the same that SPG do? Because I don't have how to access the Gtk window to read the list of repos xD But thank you for your help! – Alexandre Teles Aug 31 '12 at 16:49
Plese see my edit to open the sources list in a terminal – Mark Kirby Aug 31 '12 at 16:52
Thank you for your help. This is very usefull! – Alexandre Teles Aug 31 '12 at 16:56
nano is installed by default in Ubuntu. – Anwar Shah Aug 31 '12 at 17:53

I would just use grep, if the repository name is in REPO variable, do something like this:

if ! grep -q "$REPO" /etc/apt/sources.list; then
  # do something if repo is not installed
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.