3rd July 2009

Automatically backing up files before making changes

June 1st, 2008 | by dave |

It’s best practice (and common sense) to make a backup of a file before you edit it. Unfortunately it’s easy to forget to do this. We use this simple script below to make a time/date stamped copy of a file before launching the editor (in this case vim). We create it as /usr/local/bin/bvi.


#!/bin/bash
[[ -r $1 ]] && cp $1{,.`date +%Y%m%d-%H%M`} || echo “$1 is a new file”

vim $1

We then add the following two aliases to our ~/.bashrc file to make sure it’s run automatically when we call vim.

alias vi=/usr/local/bin/bvi
alias vim=/usr/local/bin/bvi

This isn’t a replacement for good version control of important files, but it’s a good safety net. It’s also worth noting that this can leave a lot of old copies of files laying about, so it’s work cleaning out old copies every now and again.

Tags: , , , , ,

Post a Comment