protect a file from delete

How can I set a file permission so only the user can delete it?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chmod 644 the_file

root@med1091[/tmp]> tst
root@med1091[/tmp]ls -l tst
-rw-rw-rw-   1 root       sys              0 Dec  3 13:56 tst
root@med1091[/tmp]chmod 644 tst
root@med1091[/tmp]ls -l tst
-rw-r--r--   1 root       sys              0 Dec  3 13:56 tst
so, the file can be read by owner, group, and all
can be writed (and deleted) by the owner

beware : this file situated in the directory that belong at an other user can generally be destroyed by the owner of the directory; you must in this case set the bit t on this directory (meaning only the owner of the file can delete it);
in some systems even this maneer is unsufficent, and the disrectory must belong at the owner of the file;

not very nice!

rbruno_elliottAuthor Commented:
Sorry I should have included the following diatribe

Actually I need the file to be writable but not deletable by the group and others.  I only want the user to be to completely delete the file.  I thought there was a command which allowed you to accomplished this.  Keeping in mind that if you can write to it you could essentially erase all the data.


I have a spredsheet file that is used by quite a number of people and somebody keeps deleting accidently.  Whoever is doing this is unaware that they are deleting it.  If they had write access but not delete access (completely removing the file) I think it would solve my problem.

Sorry about not being specific enough in the original question.  
What you need to do is make the directory the file resides in not writable by the offending person.

For instance
chmod 755 test/

now people can read and write files in test, but they can't delete those files.

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

rbruno_elliottAuthor Commented:
The original reply from Bedot is more what I am looking for.  Although I can not really say that this reply won't work.  We are having majot network problems and have been unable to verify anything

Setting the bit t is what I think I'm looking for.

If I do a

chmod o+g-t filename

Will this allow only the user to delete the file?

the simplest solution is the solution that has been mentioned already of using
the sticky bit "t" for the directory where
this file resides. this will allow other users/group ID
to write to the file ( with correct file permissions )
for example chmod 664 filename
chmod 1755 <directory name> ( where file resides )

Owner+ Group can write to the file, but only
owner can delete it.

The problem here is anyone who has access to
write to the file can wipeout the file without deleting the file.

I would suggest you use some kind of revision control mechanism.. say sccs, rcs, cvs etc. where a person can checkout the file modify it and checkback in when this done. This will also maintail the revisions of the file and every change made to it.

There is one more option for you.  You can write scripts or alias the commands that users use to access this file.  So that everytime someone opens the file for modification, they make a backup copy of the file before they even get to the editing part.
This way if someone deletes the file, or wipes out the data, you still  have one or more backup copy.

If you need any specific data, please let me know.
rbruno_elliottAuthor Commented:
Thanks a lot that will do the trick.
hey! what about the chintu's points????
A file on unix delete permission on unix is not controlled by the permissions of the file it self but
the permissions of the owner of the directory the file resides in.

To protect it from deletion by any other than the owner simply set the
directory permissions to :

chmod 755 user_dir

To affect the file :
 cd to user_dir and set the permissions as desired.
rbruno_elliottAuthor Commented:
Thanks sounds good to me.

I'm looking to accept Chintus answer because he was first.
He got me there already but I've been unable to go back and give him credit.

Thanks though.

rbruno_elliottAuthor Commented:
I would like to give the credit to Chintu for giving me the
first answer for exactly what I was looking for but have
been unable to do so.
I wasn't given the option to accept or reject on that 1 just to
read it.

 Also cjrd99's response was very helpful too.
If I could give them both a good grade and points I would like to.  All I need to know now is how to do that.
Use my previous comments as the answer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rbruno_elliottAuthor Commented:
Thanks a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.