36

I made a directory called "shadi" and set this permission for it

sinoosh@ubuntu:/home$ ls -ld shadi
drwxr-xrwx 2 root root 4096 Jul  1 01:58 shadi

In this situation I have "others" permissions, but why can I not delete it?

sinoosh@ubuntu:/home$ rm -r shadi/
rm: cannot remove ‘shadi/’: Permission denied
0

5 Answers 5

43

Here the problem is you do not have "w" permission on the /home directory. While deleting a file note that you are not writing to that file but you are changing the contents of the directory that contains the file, so having "w" permission on the directory is a must if you want to delete any file from the directory.

terminal screenshot of "ls -ld /home"

If I am in a directory with "w" permission, I can delete any of its files without being worried about the file permission itself. Note that my present directory is /home/rohith which has "w" permission and hence I can delete any of its files irrespective of the file permissions.

terminal screenshot of removing /home/rohith/shadi

If the same file is created in the '/home' directory which has no "w" permission I get the same output as yours ! :)

terminal screenshot of trying to remove /home/shadi

1
  • The restricted deletion flag or sticky bit 't' on a folder would also prevent deleting "it prevents unprivileged users from removing or renaming a file in the directory unless they own the file or the directory; this is called the restricted deletion flag for the directory, and is commonly found on world-writable directories like /tmp. For regular files on some older systems, the bit saves the program's text image on the swap device so it will load more quickly when run; this is called the sticky bit." Oct 5, 2021 at 4:52
26

This happens because you don't own the directory, it is owned by 'root' and the 'root' group. So to delete it you can either changing the ownership and then delete it (here you elevate your rights and become 'root' for taking the ownership):

sudo chown $USER:$USER ./shadi
rm -r ./shadi

Or you skip taking the ownership and make a sudo rm to elevate your rights and become 'root' for the deletion:

sudo rm -r ./shadi
3
  • it means just the owners can delete files?
    – Sinoosh
    Jul 1, 2016 at 10:15
  • Yes, or you need to elevate your rights with the sudo command. this will ask for your password then and you become 'root' for the particular following command.
    – Videonauth
    Jul 1, 2016 at 10:17
  • do you now if "shadi" was a file i can remove it with "sinoosh" user .it is not require "sudo " its not make scene for me why it does not work with directory
    – Sinoosh
    Jul 1, 2016 at 11:02
16

Try this:

sudo rm -r ./shadi

The permissions show owner root and group root, so you have to use sudo.

3
  • 1
    Yes this command is ok, but my problem is why rm is not working with this permissions i have full permissions for this directory in other field
    – Sinoosh
    Jul 1, 2016 at 10:06
  • "i have full permissions for this directory" No you don't. "other field" shadi is not "others".
    – Rinzwind
    Jul 1, 2016 at 10:23
  • but "sinoosh" is belongs to others and he execute "rm -r shadi"
    – Sinoosh
    Jul 1, 2016 at 10:28
4

It's different accounts.

How would you expect safety from your account, if everyone had the permission to delete your home directory?

You do not have the w permission to delete someone other's home directory by default, except if they chmod +w it.

Here are the different permissions, and an example (-rwxr-x---/rwxrx):

+-+-+-+-+
|+|u|g|o|
+-+-+-+-+
|r|1|1|1|
+-+-+-+-+
|w|2|2|2|
+-+-+-+-+
|x|4|4|4|
+-+-+-+-+
|*|7|5|0|
+-+-+-+-+
It goes like this: 1 + 2 + 4 = 7, 1 + 4 = 5, 0 = 0, so 750, i.e. an ideal permission system. The owner (u in this case) can read, write and execute the file, the owner's group (g in this case) can read and execute, and anyone other (o in this case) cannot do anything. This is the legendary:
u: current user (User)
g: current user's groups (Groups)
o: not current user's groups (Others)
r: read permission (Read)
w: write permission (Write)
x: execute permission (X-ecute)
.

2
  • 2
    This table is wrong: r (read) is 4 and x (execute) is 1! (see man chmod)
    – MaxGyver
    Feb 17, 2022 at 11:18
  • Honestly this should be the accepted answer, I don't know how the accepted answer didn't realize that the guy was trying to delete someone's account.
    – Joao-3
    Jan 28 at 14:59
1

you must get root access first by:

sudo su

then force deleting the file

rm -rf ./shadi
2
  • 3
    This is a duplicate of this answer, except your answer is more dangerous...
    – Nmath
    Oct 11, 2020 at 18:44
  • BEST ANSWER for me. That "sudo su" permission was the key which was not mentioned in the above answers.
    – Reza Taba
    May 1, 2021 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .