Linux – CHMOD To Prevent Deletion Of File Directory

chmodhostinglinuxpermissions

I have some hosting on a Linux server and I have a few folders that I don't ever want to delete. There are sub folders within these that I do want to delete.

How do I set the CHMOD permissions on the folders I don't want to delete?

Of course, when I say "I don't ever want to delete" – what I mean is that the end customer shouldn't delete them by accident, via FTP or in a PHP script etc.

As an example of directory structure…

MainFolder/SubFolder
MainFolder/Another

I don't want "MainFolder" to be accidentally deleted, but I'm happy for "SubFolder" and "Another" to be removed!

Best Answer

Deleting a file/directory changes the contents of the parent directory, hence, if you don't want MainFolder to be deleted, you want to ensure that the intended user does not have write access to the parent dir of MainFolder.

Assuming this structure: /some/dir/ParentDir/MainFolder/SubFolder

You'll want to run something like this to prevent deletion (for all users):

chmod a-w /some/dir/ParentDir

Of course, this is not an ideal situation as making it non-writeable means than users cannot add additional files/directories to /some/dir/ParentDir

Would a sticky bit fit your purpose better? setting the sticky bit on the parent directory will only allow deletion by the directory owner.

chmod +t /some/dir/ParentDir

Look at the usage section on http://en.wikipedia.org/wiki/Sticky_bit for more information about Sticky bits.

Related Topic