2

I'm a user (not root) on a RHEL server. I want to be able to continue editing a file in my home directory, but set a permission so I don't accidentally delete it. Is this possible? Can I make it so I need to be secondarily prompted before deleting it (eg. "Are you sure? y/n")?

  • what'd be the difference between "editing it" down to zero bytes and deleting it? – Jeff Schaller Aug 4 '16 at 13:23
  • I'm more confident that I won't edit it down to zero bytes. – Angus Aug 4 '16 at 13:24
  • 4
    In a startup file, such as .bashrc, you could add an alias alias rm='rm -i' so that you'll always get the 'are you sure' prompt. It can be dangerous to start depending on this behaviour; better to make typing rm -i a muscle habit. – user4556274 Aug 4 '16 at 13:24
  • You may be looking for the wrong solution, you may be better with revision control (subversion (svn), mercurial (hg) – ctrl-alt-delor Apr 10 '17 at 12:05
3

You remove a file by unlinking it to the directory it's referenced in (a file could also be referenced in more than one directory, or several times (with different names) to one directory, that's what we usually call hard links).

The permissions that matter then are not that of the file, but that of the directory it's linked to.

So if you place it in a directory you don't have write access to:

mkdir important-files
echo test > important-files/myfile
chmod a-w important-files # make the directory not writable

Then you won't be able to delete myfile, but you'll still be able to modify the file as long as it's writable.

2

If you create a subdirectory, and remove write access from yourself, you won't be able to remove any files in the directory, but you can still modify them.

$ mkdir dir ; echo something > dir/foo ; chmod a-w dir
$ $EDITOR dir/foo
$ rm -rf dir/
rm: cannot remove 'dir/foo': Permission denied

Though you could still destroy or truncate the contents while editing it, so a better option would be to have backups. Your system administrator could (should?) take backups into a directory you couldn't delete, but a cheap simulation could be done with basically the same idea as above: Make a directory, and create a script that temporarily restores the write permission, makes backup copies into the directory and clears the write permission again.

  • And you can't add new files while write permission is denied. – Murphy Aug 4 '16 at 14:15
2

I want to be able to continue editing a file in my home directory, but set a permission so I don't accidentally delete it. Is this possible?

In short, not per file with the usual Unix permissions. Perhaps ACLs can provide such fine-grained control?

Can I make it so I need to be secondarily prompted before deleting it (eg. "Are you sure? y/n")?

A common workaround for this is setting an alias for rm to rm -i, e. g. in ~/.bash_aliases (if supported by your distribution), ~/.bashrc or even globally in /etc/bashrc (which may be overwritten by updates of the bash package, so better don't use this option):

$ cat .bash_aliases 
alias rm='rm -i'
...
1

I'd use hardlinks with a backup folder, where I would keep a hardlink to each file I want to stay undeletable, just in case the deletion takes place. In case you delete the file, you can then create a hardlink from the backup dir to your home folder. This is not a perfect solution, but a relatively safe one.

$ mkdir filebak
$ echo content >file
$ ln file filebak/file
$ rm file
$ ln filebak/file file
$ cat file
content
1

This only works for one file you specify in the myFile variable.

#
# Wrapper around /bin/rm to prevent accidental `rm myFile`
#
rm (){
local rm=$(which rm)

myFile="important.txt"

if [ -f "$myFile" ]; then
  $rm -iv "$myFile"
else
  $rm "$@"
fi

}

Remember to add this to /etc/profile then ./etc/profile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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