4

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!

Fenton
  • 224
  • 2
  • 4
  • 15

3 Answers3

11

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.

Shawn Chin
  • 1,854
  • 1
  • 11
  • 12
  • 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! – Fenton Mar 23 '10 at 11:03
  • Hey Shawn I'm having pretty much the exact issue Fenton had, expect, this is not working. I have a `Backup` directory and each user is supposed to have their own folder in there. But no matter what I do, the user can still delete their folder using SMB. Backup permission: `dr-x---r-t 1 admin admin`, folder inside backup: `drwxr-xr-x 1 Elias users`. I have to be missing somethign! If I remove the `w` permission from the inner folder, the user is obviously not able to add content anymore. – Elias Feb 14 '21 at 20:16
0

You could change the owner of MainFolder with chown

Oli
  • 1,799
  • 19
  • 27
0

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.