Is there a way to prevent files to be overwritten, if the file with the same name already exists?
Either it should ask for root password to confirm or simply save it with a new name, like windows does, adding numbers to names.

- 367
- 3
- 16
1 Answers
The best method would be for you to learn to create a copy yourself before editing a file.
That said ...
Is there a way to prevent files to be overwritten
Yes. From command line set the "immutable" attribute (only the admin can remove that option) and nobody will be able to alter the file (edit, remove, move etc.). You do that with ...
sudo -i
{password}
chattr +i {file}
(-i
to remove it)
Either it should ask for root password
Ubuntu does not have a use able root password.
You can modify a file (even if it's read-only) if you own it and have write access to the directory. See for instance Why can I modify a read-only file? on how to save from vim. It will then complain that you need to take an extra action before you can save it.
Not exactly what you asked but it is a method to get some sort of notification before you overwrite it.
or simply save it with a new name, like windows does, adding numbers to names.
You will need a script for that when doing that on command line. Here are some examples: https://stackoverflow.com/questions/12187859/create-new-file-but-add-number-if-filename-already-exists-in-bash

- 299,756
-
I find myself saving documents from web. Some times i have accidentally overwritten them, if it was already saved or the name was same. That script assumes only single file would be considered. Is there any other way? Can it be automated? – arjun Mar 14 '18 at 17:55
-
And the
chattr
command, is it safe to do it system wide for all user files or specific folder? Can it be done? – arjun Mar 14 '18 at 17:58
400
– You'reAGitForNotUsingGit Mar 14 '18 at 15:29