Penguin
Note: You are viewing an old revision of this page. View the current version.

Append to a file owned by root

In this example I will use the sources.list file that contains a list of the repositories from which we can get packages.

Firstly:

  • sudo echo "#TEST REPOSITORY" >> /etc/apt/sources.list

Results in a "Permission denied error". Why is this? I have no idea, can anyone help out? :D

Ways around this:

1)

  • sudo bash -c 'echo "#TEST REPOSITORY" >> /etc/apt/sources.list'

Everything after the -c switch is read into bash.

2)

  • echo "#TEST REPO" | sudo tee -a /etc/apt/sources.list

The 'tee' command reads from standard input and the -a switch appends it to the file.

3)

  • sudo tee -a /etc/apt/sources.list <<< "#TEST REPO"