3

Suppose you have permission to write to a file but not to delete it.

What rights do we talk about?

Read and write rights?

  • 2
    Not sure what you're talking about, but askubuntu.com/questions/240424/… – muru May 21 '18 at 7:45
  • 1
    Reopen voters: I think it's clear what OP is asking but wording could be better. Judging from answer up-votes it's a good question but might be a duplicate as Muru alludes. – WinEunuuchs2Unix May 22 '18 at 13:02
14

To write to an existing file you need write permissions for that file.

To delete a file you need write permission for the folder that contains that file.

  • 2
    As an aside: rm asks first when one tries to delete a write protected file, even when the directory permissions are sufficient. – Peter A. Schneider May 21 '18 at 13:47
  • As another aside, you can force write to a file itself if you have write permission on the directory - for example in Vim you can use :w! – Zanna May 22 '18 at 14:42
4

Florian's answer is not quite complete on my system: I need both write and execute permission on the directory in order to delete a file in it.

Sample session:

I'm running a well-seasoned Debian with the ext4 file system. I create a directory with a file in it and then change the directory and file permissions before I try to delete the file.

File system ext4:

testuser@www:~$ df -T .
Filesystem     Type 1K-blocks     Used Available Use% Mounted on
/dev/sda3      ext4 152326340 44429904 100196624  31% /

Let's create a directory and a file in it:

testuser@www:~$ mkdir d
testuser@www:~$ ls -ld d
drwxr-xr-x 2 testuser testuser 4096 May 21 16:19 d
testuser@www:~$ echo kjhkjh > d/f
testuser@www:~$ ls -l d/f
-rw-r--r-- 1 testuser testuser 7 May 21 16:19 d/f

Expected: Cannot change file catalog without write permission on it:

testuser@www:~$ chmod 100 d && ls -ld d
d--x------ 2 testuser testuser 4096 May 21 16:19 d
testuser@www:~$ rm d/f
rm: cannot remove 'd/f': Permission denied

Unexpected: Cannot change file catalog without execute permission on it:

testuser@www:~$ chmod 200 d && ls -ld d
d-w------- 2 testuser testuser 4096 May 21 16:19 d
testuser@www:~$ rm d/f
rm: cannot remove 'd/f': Permission denied

I need both write and execute permission on it:

testuser@www:~$ chmod 300 d && ls -ld d
d-wx------ 2 testuser testuser 4096 May 21 16:19 d
testuser@www:~$ rm d/f

As an aside: When the file (but not the directory) is write protected rm asks for permission before deleting. After all, deleting a file can be considered an extreme form of writing to it. There is no difference in lost data between echo -n "" > d/f and rm d/f in the common case that there is no other hardlink to the file.

testuser@www:~$ !echo
echo kjhkjh > d/f
testuser@www:~$ chmod 700 d && ls -ld d
drwx------ 2 testuser testuser 4096 May 21 16:21 d
testuser@www:~$ chmod 000 d/f && ls -l d/f
---------- 1 testuser testuser 7 May 21 16:21 d/f
testuser@www:~$ rm d/f
rm: remove write-protected regular file 'd/f'? y
testuser@www:~$  ls -l d
total 0
testuser@www:~$
  • I think one always needs execute/traversal permission on a directory and all its parents to do anything with its content in addition to read or write permissions. – David Foerster May 22 '18 at 19:19
  • @DavidFoerster Well, that's usually not mentioned; neither in the accepted answer here nor in most other places. And it was new to me. The execute bit on directories is rarely well explained (do you have a canonical documentation? I couldn't find any within the 2 minutes I gave it). I't also only medium logical, I find. It also seems redundant: What can I then do with just write but not execute permission? – Peter A. Schneider May 23 '18 at 0:38
  • Nothing. In a way the traversal permission (i. e. the execute flag on directories) effectively affects the children and grand-children and not the directory with that permission itself. See askubuntu.com/q/862289/175814 and the links in its comments for further reading. – David Foerster May 23 '18 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy