Page 1 of 1

Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 12:50 pm
by darkstormplayz
So I am trying to delete all the text within a TXT file using this command

Code: Select all

echo " " > _autoplaylist.txt
I then get a

Code: Select all

-bash: _autoplaylist.txt: Permission Denied
error I get the same error even using sudo.

Plase help.

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 2:01 pm
by ChipTechno
Do you own the file ? Can you post the result of

Code: Select all

ls -l _autoplaylist.txt
?

Try

Code: Select all

chown pi;pi _autoplaylist.txt
and

Code: Select all

chmod 775 _autoplaylist.txt
and run your command again.

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 4:45 pm
by Paeryn
Also, are you in a directory where you have write permission? Typically any directory outside of your home directory (and it's sub-directories) are not writable by a normal user.

Just putting sudo in front of the command won't have any effect on whether the redirection can happen, sudo only runs echo as root, the redirection is done by the calling shell before the command is run and so is still being done as the user. Basically the shell opens (or creates) the file _autoplaylist.txt first and then runs sudo echo " " with its output set to the file rather than the terminal.

If you need to have the redirection applied with root permission (really don't do this unless you have to or know what you are doing, it generally isn't the correct way)

Code: Select all

sudo sh -c 'echo " " > _autoplaylist.txt'

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 4:53 pm
by jahboater
Once you have the permissions sorted out, you can save some typing:

> _autoplaylist.txt

is all you need:

Code: Select all

[email protected]:~ $ ls -l myfile
-rw-r--r-- 1 pi pi 362212 Dec 18 16:57 myfile
[email protected]:~ $ >myfile
[email protected]:~ $ ls -l myfile
-rw-r--r-- 1 pi pi 0 Dec 18 16:57 myfile
[email protected]:~ $ 

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 5:13 pm
by hortimech
Try:

[code}
sh -c 'echo | sudo tee -a _autoplaylist.txt'
[/code]

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 5:14 pm
by hortimech
And, of course it should have been

Code: Select all

sh -c 'echo | sudo tee -a _autoplaylist.txt'

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 5:19 pm
by n67
hortimech wrote:
Tue Dec 18, 2018 5:14 pm
And, of course it should have been

Code: Select all

sh -c 'echo | sudo tee -a _autoplaylist.txt'
I see this "sudo tee -a" hack a lot on this board. Gosh, is it ugly. There's so many better ways...

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 5:42 pm
by mfa298
n67 wrote:
Tue Dec 18, 2018 5:19 pm
hortimech wrote:
Tue Dec 18, 2018 5:14 pm
And, of course it should have been

Code: Select all

sh -c 'echo | sudo tee -a _autoplaylist.txt'
I see this "sudo tee -a" hack a lot on this board. Gosh, is it ugly. There's so many better ways...
the "sudo tee" trick isn't that bad a method although the version above won't do much. The -a for tee is append to file so that will just add a blank line to the end of the file. Personally I find it less ugly than the "sudo sh -c 'echo > file'" method that is the usual alternative. The other benefit with tee is it works with heredocs for multiline additions ("sudo tee /myfile << _EOF" and then the lines to add ending with _EOF)

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 7:44 pm
by Roken
Do you possibly have the immutable flag set?

Code: Select all

sudo chattr -i path/to/file

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 8:22 pm
by PeterO
n67 wrote:
Tue Dec 18, 2018 5:19 pm
There's so many better ways...
If you aren't going to help why do you post replies to questions ?
PeterO

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 9:08 pm
by n67
PeterO wrote:
Tue Dec 18, 2018 8:22 pm
n67 wrote:
Tue Dec 18, 2018 5:19 pm
There's so many better ways...
If you aren't going to help why do you post replies to questions ?
PeterO
Like you did?

Re: Permission denied error even when using sudo.

Posted: Tue Dec 18, 2018 9:13 pm
by mfa298
n67 wrote:
Tue Dec 18, 2018 9:08 pm
PeterO wrote:
Tue Dec 18, 2018 8:22 pm
n67 wrote:
Tue Dec 18, 2018 5:19 pm
There's so many better ways...
If you aren't going to help why do you post replies to questions ?
PeterO
Like you did?
I suspect PeterO was suggesting that maybe if there are so many better ways you should be helpful and list them and maybe a line about why they're better. As with most things Unix there are multiple ways of doing a task sometimes it's down to personal preference sometimes people may just have not seen a better way of doing something (for me I like sudo tee as it works on most systems I use - some of the other hacks are Linux only)

Re: Permission denied error even when using sudo.

Posted: Wed Dec 19, 2018 2:15 am
by Paeryn
I don't like the tee method because tee will write its input to the file and to stdout. Granted for something short on the command line the user probably won't mind. It all depends on what you are used to and what is most convenient for the task at hand.

Re: Permission denied error even when using sudo.

Posted: Wed Dec 19, 2018 7:37 pm
by rhubarbdog
Try making the file executable. That's what your trying when you type ./_autoplay-list
Have you used

Code: Select all

ls -l _autoplay-list
to detetmine who owns it and what the permission are.

Re: Permission denied error even when using sudo.

Posted: Wed Dec 19, 2018 8:26 pm
by code_exec
darkstormplayz wrote:
Tue Dec 18, 2018 12:50 pm
So I am trying to delete all the text within a TXT file using this command

Code: Select all

echo " " > _autoplaylist.txt
I then get a

Code: Select all

-bash: _autoplaylist.txt: Permission Denied
error I get the same error even using sudo.

Plase help.
Try:

Code: Select all

chmod +x _autoplaylist.txt

Re: Permission denied error even when using sudo.

Posted: Wed Dec 19, 2018 9:42 pm
by n67
It is unlikely that this is a "make it executable" problem, given that:

1) It is a TXT file (according to the OP)
2) He is trying to delete all the text from the file.

Re: Permission denied error even when using sudo.

Posted: Thu Dec 20, 2018 7:58 am
by rpdom
n67 wrote:
Wed Dec 19, 2018 9:42 pm
It is unlikely that this is a "make it executable" problem, given that:

1) It is a TXT file (according to the OP)
2) He is trying to delete all the text from the file.
If you aren't going to help why do you post replies to questions?

For the purposes of deleting the contents of a file, this should work (unlike my keyboard, which is being annoying today):

Code: Select all

sudo truncate -s 0 _autoplaylist.txt
I'm not sure of the file's ownership in this particular case. Normally sudo etc wouldn't be needed, but it looks like it might be here.

Re: Permission denied error even when using sudo.

Posted: Thu Dec 20, 2018 3:57 pm
by DougieLawson
Has anyone asked the OP where the file exists? Is it on a mounted filesystem? Is it on NTFS without ntfs-3g (for example) which would make the filesystem read-only? Is it on a shared (CIFS or NFS) filesystem that may be mounted read-only?

We've got none of that context information from the OP.