Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a non-root user "joshua", as root I created a file called "foo" at joshua's home directory (/home/johsua/); it look like this:

-rw-r--r--  1 root   root       0 12-19 21:00 foo

and then delete it as joshua, i can delete it successfully.

I would expect that joshua doesn't have enough permission to delete it. Is it some kind of 'Permissions inheritance'? My platform is Debian 5.0.7.

share|improve this question

migrated from Dec 19 '11 at 13:37

This question came from our site for professional and enthusiast programmers.

1) This is off-topic. 2) I very much doubt that you were able to delete that as non-root user without sudo or other privilege escalation. You probably overlooked something. – DarkDust Dec 19 '11 at 13:11
@DarkDust A file is an entry in its parent directory. If you have write rights in a directory, you can remove files in it regardless of who owns the files (unless the sticky bit is also set on the directory) – nos Dec 19 '11 at 13:13
@nos: Silly me, you're right. – DarkDust Dec 19 '11 at 13:14
An entry in its parent directory is a reference to a file. It is not the file itself. (Otherwise, how could a file be hard linked to more than one directory?) – David Schwartz Dec 19 '11 at 14:59
up vote 38 down vote accepted

The user didn't delete the file, the system did. The user merely removed the file from his own directory. The system deleted the file because its reference count dropped to zero. It's just happenstance that the user removing the file from the directory happened to drop its reference count to zero. (If the file was hard linked to another directory or a handle was opened to the file, it would not have been deleted.)

The system deletes files automatically when their reference counts drops to zero. The owner of the file doesn't matter. There are many ways someone other than the owner of a file can drop the file's reference count to zero.

Removing a file from a directory (called 'unlinking') is an operation on the directory. Unlinking a file reduces its reference count.

Similarly, a user other than the owner could close the last handle to a file that isn't linked to any directories. Closing that handle would delete the file as well, since again the reference count would drop to zero.

share|improve this answer
And of course the rm command muddies the waters a bit, because rm is an abbreviation of "remove", and users are trained to think of rm as a "delete" operation. Many users use rm every day while being unaware that the operation it actually performs is an "unlink", not a "delete". As a result, it shouldn't really be surprising that many users find this behavior surprising when they first encounter it. – Daniel Pryden Dec 20 '11 at 1:32
It is definitely surprising to many people. At least the rm command does in fact remove a file or directory from a directory. It's worse on Windows where the command is called del, because it used to delete a file but on modern Windows machines (since NT4), it's also an unlink operation. – David Schwartz Dec 20 '11 at 2:21

Deleting files is an operation on the directory: Any user who has write-permissions on the directory can delete contained files (unless the sticky bit (T or & 01000) is set on the directory).

share|improve this answer
You mean unlinking the file is an operation on the directory. Deleting the file is an operation on the file. (And you cannot delete a file without write permission on every directory that contains that file.) – David Schwartz Dec 19 '11 at 14:54
@David Right, but that doesn't matter. This was a question about permissions. Not unlinking vs deleting. Those terms are used interchangeably by many people. – Ryan Babchishin Oct 6 '15 at 13:49
@RyanBabchishin Well, the use of those terms interchangeably is part of the problem since they're two completely different operations. Deleting a file is not an operation on a directory. Unlinking a file from a directory certainly is. – David Schwartz Oct 7 '15 at 12:47

First guess: For deleting a file you need write permissions on the containing folder. So Try /home/johsua/foo/bar, give 755 to foo and 644 to bar.

share|improve this answer

You must log in to answer this question.

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