What permissions are needed to delete a file in unix?

Ahmed Khan picture Ahmed Khan · Feb 11, 2019 · Viewed 11.2k times · Source

I currently have a directory (udir), which has only read and write permissions for all users. This directory contains two files (file1 & file2)

I initially though that only write access was needed (on the directory) for me to be able to delete/remove a file via (rm udir/file1) but the rm command would give me access denied. when i set the permissions to read, write, and execute, the rm command works.

Obviously the execute access is needed as well but why??

I thought the execute access on a directory was to be able to make it a working a directory and search its contents and access sub directories.

Answer

nullptr picture nullptr · Feb 11, 2019

You actually need read and write permissions on the directory, not on the file itself since the operation is done considering the permissions effects of directories.

A good documentation can be found on this link, which mentions the below in the section Special Considerations on Directories:

To delete a file requires both write (to modify the directory itself) and execute (to stat() the file's inode) on a directory.  Note a user needs no permissions on a file nor be the file's owner to delete it!