darkstormplayz
Posts: 4
Joined: Sat Dec 26, 2015 9:01 pm

Permission denied error even when using sudo.

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.

ChipTechno
Posts: 2
Joined: Tue Dec 18, 2018 1:33 pm

Re: Permission denied error even when using sudo.

Tue Dec 18, 2018 2:01 pm

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.

User avatar
Paeryn
Posts: 2604
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Permission denied error even when using sudo.

Tue Dec 18, 2018 4:45 pm

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'
She who travels light — forgot something.

jahboater
Posts: 4591
Joined: Wed Feb 04, 2015 6:38 pm

Re: Permission denied error even when using sudo.

Tue Dec 18, 2018 4:53 pm

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]:~ $ 

hortimech
Posts: 286
Joined: Wed Apr 08, 2015 5:52 pm

Re: Permission denied error even when using sudo.

Tue Dec 18, 2018 5:13 pm

Try:

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

hortimech
Posts: 286
Joined: Wed Apr 08, 2015 5:52 pm

Re: Permission denied error even when using sudo.

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'

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Permission denied error even when using sudo.

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...
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Permission denied error even when using sudo.

Tue Dec 18, 2018 5:42 pm

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)

Roken
Posts: 288
Joined: Sun Dec 31, 2017 4:35 pm
Location: UK

Re: Permission denied error even when using sudo.

Tue Dec 18, 2018 7:44 pm

Do you possibly have the immutable flag set?

Code: Select all

sudo chattr -i path/to/file
Headless PI. OMG, someone cut it's head off. Oh, hang on. it didn't have one to start with.

User avatar
PeterO
Posts: 4867
Joined: Sun Jul 22, 2012 4:14 pm

Re: Permission denied error even when using sudo.

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Permission denied error even when using sudo.

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?
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Permission denied error even when using sudo.

Tue Dec 18, 2018 9:13 pm

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)

User avatar
Paeryn
Posts: 2604
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Permission denied error even when using sudo.

Wed Dec 19, 2018 2:15 am

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.
She who travels light — forgot something.

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: Permission denied error even when using sudo.

Wed Dec 19, 2018 7:37 pm

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.

code_exec
Posts: 271
Joined: Sun Sep 30, 2018 12:25 pm

Re: Permission denied error even when using sudo.

Wed Dec 19, 2018 8:26 pm

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
Ubuntu 18.04 LTS desktop images for the Raspberry Pi 3.

https://github.com/CodeExecution/Ubuntu-ARM64-RPi

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Permission denied error even when using sudo.

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.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
rpdom
Posts: 14701
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Permission denied error even when using sudo.

Thu Dec 20, 2018 7:58 am

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.

User avatar
DougieLawson
Posts: 35519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Permission denied error even when using sudo.

Thu Dec 20, 2018 3:57 pm

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.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Troubleshooting”