2

I am trying to append some config to another file like this

sudo cat config/add-this.yml >> ~/docker-compose.yml

via shell script. But trying this gives me a Permission denied error.

How can I simply append some content to another file?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
user3142695
  • 1,529
  • 7
  • 20
  • 34

1 Answers1

1

The problem is that the shell performs redirections before the command is executed.

In this case. unless the permissions of the file into which data is to be added allow appending, a permission denied error results.

You can circumvent this by doing:

sudo sh -c 'cat config/add-this.yml >> ~/docker-compose.yml'
JRFerguson
  • 14,570
  • 3
  • 34
  • 40
  • So it would also work if I would set `chmod 777 docker-compose.yml`, right? – user3142695 Feb 10 '17 at 22:32
  • Yes, though opening permissions is often the wrong approach. Some configuration files **must** have r/w permissions and only by the owner for a daemon to accept the file for processing. – JRFerguson Feb 10 '17 at 22:54
  • What does the `-c`-flag do? – user3142695 Feb 10 '17 at 22:59
  • `-c` reads and executes commands from the first non-option argument command_string, and then exits. See [here](http://www.gnu.org/software/bash/manual/bash.html#Invoking-Bash) . – JRFerguson Feb 10 '17 at 23:23