0

Frequently I forget to backup the original copies of important system files I’m messing around with.

Is there any way to have the system automatically copy a file that I edit, if e.g. it’s under /etc ? Just make a copy of filename to filename~ in the same directory?

1

It will depend upon which editor you use:

  • nano has an option for automatically backup files when you save them, which can be in the same or alternate directory.
  • gedit has an option [to not] save a backup file
  • vim apparently does as well

It would be best to determine which editor that you would like to use, and research the documentation of that editor to determine how and where, it will make backups of the files that you edit.

0

If your editor does not provide an option for that you could write a function like that one:

kate~(){ cp "${!#}" "${!#}~" && kate $@ ;}

You can do that for every editor, this simply takes the last argument (${!#}), which always should be the filename, makes a copy of it and runs the editor (kate) with the whole argument line ($@). This example function is called with kate~ [OPTIONS] [FILE]. Functions like that are best stored in the ~/.bash_aliases file, this way they are effective for every new opened terminal.

To test for a specific directory like /etc/ you could do e.g.

kate~(){ [[ "${!#}" =~ /etc/ ]] && cp "${!#}" "${!#}~" ; kate $@ ;}

This way the backup file will only be created if the file to open is located under /etc/.

  • So in your example, ‘kate’ is just some arbitrary function name, and the file name on this script in ~/.bash_aliases folder? So if I wanted to overload vim, I should do all this but replacing Kate with vim? – EE1337 Oct 5 '17 at 6:57
  • No, kate~ is the arbitrary function name and kate is the editor command. So you could do arbitrary_function_name(){…; vim $@ ;} – dessert Oct 5 '17 at 7:01

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.