Q . Linux user(owner) how can delete a file when doesn't have any permission on that file ?

Dev Singh. asked, Nov 05 ' 2016

I created a file name test.txt

I removed all my permissions from that file. Than how can I delete the same file. Below are the commands from which I remove the permissions.

dev@dev-singh:~/test$ touch test.txt
dev@dev-singh:~/test$ ls -l
total 0
-rw-rw-r-- 1 dev dev 0 Nov  5 20:48 test.txt
dev@dev-singh:~/test$ chmod u-rwx test.txt 
dev@dev-singh:~/test$ ls -l
total 0
----rw-r-- 1 dev dev 0 Nov  5 20:48 test.txt
dev@dev-singh:~/test$ ls -l
total 0
----rw-r-- 1 dev dev 0 Nov  5 20:48 test.txt
dev@dev-singh:~/test$ rm test.txt 
rm: remove write-protected regular empty file 'test.txt'? y
dev@dev-singh:~/test$ ls -l
total 0
dev@dev-singh:~/test$ 

 

  • linux
  • delete-file
  • permissions

1 Answers

The ability to delete a file has nothing to do with the actual permissions on the file. It's the permissions of the directory that contain the file that govern this.

Example

$ whoami
lee

lee@lee-stan:~$ chmod u-rwx test/

lee@lee-stan:~$ ls -l
total 48
d---rwxr-x 2 lee lee 4096 Nov  5 20:59 test
lee@lee-stan:~$ rm test/test.txt
rm: cannot remove 'test/test.txt': Permission denied
lee@lee-stan:~$ 

So just remember that the ability to read/write/execute a file belong to the file itself through its attributes. However the ability to delete a file from a directory is controlled by the permissions of the directory containing the file.

Leave a Ansewer