Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect output to a file permission denied?

I want to do a simple redirection. When I do

sudo curl <insert link here> > a.txt

I want to take all of the data outputted by the curl into a.txt. However, I keep getting an error saying

a.txt: Permission denied

Would anyone have any idea how to get around this? I've tried looking online by doing

sudo bash -c curl <insert link here> > a.txt

and that displays the same error. Any help would be appreciated! Thanks!

like image 357
user1871869 Avatar asked May 15 '13 19:05

user1871869


People also ask

How do I fix Permission denied in Linux terminal?

To fix the permission denied error in Linux, one needs to change the file permission of the script. Use the “chmod” (change mode) command for this purpose.

How do I redirect an output to a file?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.


1 Answers

The privilege elevation only applies to the curl process (and, in the second example, the child shell) itself, not to your (parent) shell, and therefore not to the redirection.

One solution is to do the redirection within the child shell itself:

sudo bash -c "curl $LINK >a.txt"

Another, fairly idiomatic option is to use tee:

curl $LINK | sudo tee a.txt >/dev/null

For curl specifically, you can also make the process itself write to the file directly:

sudo curl -o a.txt $LINK
like image 130
Cairnarvon Avatar answered Sep 30 '22 16:09

Cairnarvon