Linux – cannot remove owned file with 777 permissions

31

4

I've couple of files that I cannot remove using rf -Rf command. I'm the owner of those file and the group assigned to those files is also a group my user is in. What's even weirder is that I can edit their content and I can change the permissions for them, but I can't move or delete them.

ls -al
total 12
dr-xr-xr-x 3 rayell pg1083760 4096 2010-10-10 10:00 .
drwxr-xr-x 3 rayell pg1083760 4096 2011-09-02 04:33 ..
-rwxrwxrwx 1 rayell pg1083760    0 2011-09-02 06:38 default.settings.php
drwxrwxrwx 2 rayell pg1083760 4096 2011-09-02 04:33 files
-rwxrwxrwx 1 rayell pg1083760    0 2011-09-02 06:38 settings.php


rm -Rf *
rm: cannot remove `default.settings.php': Permission denied
rm: cannot remove directory `files': Permission denied
rm: cannot remove `settings.php': Permission denied

Can anyone tell me what is happening?

RaYell

Posted 2011-09-02T13:45:09.080

Reputation: 994

5In general, consider using rm -rf DIRECTORY_NAME and not "*". It will save you from that one time when you will think yourself in directory /some/where/safe but are instead in /home – Sardathrion - against SE abuse – 2011-09-02T15:56:04.127

Actually I did try that, it was just those 3 files that I couldn't remove so I have simplified to procedure a bit for this post's purpose. – RaYell – 2011-09-03T06:17:50.213

Don't forget the lsattr and chattr command... – None – 2014-03-13T05:36:44.983

Answers

62

To remove one file you need write permission on the directory that contains¹ this file.

Here the permissions are dr-xr-xr-x 3 rayell pg1083760 4096 2010-10-10 10:00 . So nobody (other than root) can remove files inside this directory. The owner must use chmod first.


1. There are pretty good reasons for that. By ‘removing’ a file with rm, you are in fact trying to unlink it from the directory (hardlinked copies will not be deleted).

Stéphane Gimenez

Posted 2011-09-02T13:45:09.080

Reputation: 1 239

1

Another possible question is attribute

lsattr file

This command will show you attribute of the file and a file with 'i' attribute cannot be modified (and be deleted)

so check your file's attribute and remove 'i' attribute if the attribute is been set

chattr -i file

LJP-TW

Posted 2011-09-02T13:45:09.080

Reputation: 21

2The question shows that it's the directory permissions that don't allow writing. No need to look further. – Toby Speight – 2016-04-15T19:30:02.723

1

I had the same problem, and chmod alone didn't do the trick. I first had to change the owner (user and group) of the files I wanted to remove.

sudo chown -hR root:admin dir_to_delete

Explanation:

  • sudo: make sure you have the proper rights
  • chown: Linux command to change owner of a file
  • -hR: change owner of directory and all subdirectories. I found it here.
  • root: name of new user
  • admin: name of new group

I had already changed the modifiers to 777; I don't know if that was necessary or not.

physicalattraction

Posted 2011-09-02T13:45:09.080

Reputation: 211

Permissions of files within directory are irrelevant, only write permission on directory itself matters. Probably before chown you had been neither owner of directory nor member of directory owning group. – sgnsajgon – 2017-06-06T20:45:30.467

0

The reason is that the parent directory has chmod 705 or something like that. You can chmod the parent directory by the following command:

chmod -R 777 directory_name

After this, you can remove that directory and the files it contains.

Tomagen team

Posted 2011-09-02T13:45:09.080

Reputation: 17

4Please don't condone making directories writable by everybody. That's not good advice. – Toby Speight – 2016-04-15T19:29:17.337

+1 - Agree, do not follow this advice, it's amazing, how this is plastered all over the internet... The amount of insecurity, caused by people advocating this is incredible... – André Figueira – 2017-01-12T11:08:41.277