Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

migrated from stackoverflow.com Mar 23 '10 at 10:53

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

3 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
I think I'm going to be lucky with my directory structure - I have a "ParentDir" that essentially contains lots of "MainFolders" that I don't want deleted. Any user-interaction then happens in the next level down. Awesome! I'll try it now! –  Steve Fenton Mar 23 '10 at 11:03
add comment

You could change the owner of MainFolder with chown

share|improve this answer
    
Still doesn't stop someone from deleting it, as that's governed by the parent directory. –  Vatine Mar 23 '10 at 11:54
add comment

As Oli says, you can chown -R new_owner Mainfolder to ensure all the subdirectories have a different owner to the one that'll be accessing it. That should stop other users deleting it.

To allow other users to access it, you can also create a group and change the file group with chgrp. That way, any members of your "customers" group can access the folders. The alternative is to set the other bit to read if you want everyone to have access.

Note that chmod -R / chown -R / chgrp -R / chcon -R set permissions recursively.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.