Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried "chattr +i DIRNAME", it's great, but I cannot create files in the DIR after chattr. What else are there to prevent users from deleting a Directory?

root@HOST ~] mkdir test
[root@HOST ~] chattr +i test
[root@HOST ~] cd test
[root@HOST ~/test] touch sth
touch: cannot touch `sth': Permission denied
[root@HOST ~/test] cd ..
[root@HOST ~] chattr -i test
[root@HOST ~] cd test
[root@HOST ~/test] touch sth
[root@HOST ~/test] 

UPDATE: only the directory deletion is important, so I still need the files to be deleted in it

share|improve this question
I updated the question :) – gasko peter Nov 9 '12 at 6:35
Take a look at /tmp and /var/tmp. While every user can create files there, he/she can delete only his own files/directories, but not the directory itselv. – Nils Nov 9 '12 at 21:50
up vote 4 down vote accepted

So what is wrong with a simple chown/chmod?:

cd /tmp
mkdir question
sudo chown root:root question
[sudo] password for user: 
chmod 777 ./question
touch sth
rm sth
cd ..
rm question -rf
rm: cannot remove `question': Operation not permitted

OK, let me tell you what is wrong with this: every user has all access to every file in the question directory due to the 777 permissions. It is better to

  • create a new group groupadd question
  • mkdir question
  • chown root:question ./question
  • chmod 770 ./question
  • add the users that must have access to the files to the new group: usermod -G group user

The important trick here is that the directory has a different owner than any of the users that will try to delete it.

share|improve this answer
It depends also where this directory is located. If somebody from question group has rwx rights on the directory containing question directory it will be able to delete it. – Laurentiu Roescu Nov 9 '12 at 8:48
Add an extra sudo chmod o+t .. to set the t flag on the parent directory. The t flag will disallow to remove a file that is not owned by the user. t is commonly used on /tmp to do exactly that. UserA cannot delete files owned by UserB. – jippie Nov 9 '12 at 19:50
Or once again just put the correct ownership an permission bits on the directory. – jippie Nov 9 '12 at 19:52

Create a file ".protected" and do as root

chattr +i .protected

You can then delete all files except .protected within this directory, thus the directory can't be deleted by any other user.

share|improve this answer
Simple, and effective. Great answer. – Wug May 30 at 5:10
chattr +a

should do the job. You can create files inside but you won't be able to delete them.

share|improve this answer
i updated the question – gasko peter Nov 9 '12 at 7:36

Your Answer


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.