Redirecting output to file permission prohibited?

I want to do a simple redirect. When i do

sudo curl <insert link here> > a.txt 

I want to take all the data output by curl in a.txt. However, I keep getting the error message

 a.txt: Permission denied 

Can anyone think how to get around this? I tried to look on the Internet, doing

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

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

+4
source share
2 answers

Privilege escalation applies only to the curl process (and in the second example, to the child shell), and not to your (parent) shell, and therefore to the redirect.

One solution is to redirect inside the child shell itself:

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

Another rather idiomatic option is to use tee :

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

For curl in particular, you can also write the process itself directly to a file:

 sudo curl -o a.txt $LINK 
+19
source

I ran into the same problem because the folder with the a.txt file is not writable. Either chmod/chown he, or placed it in a folder for recording, will solve the problem.

Hope this helps!

0
source

Source: https://habr.com/ru/post/1481028/


All Articles