Vi copy script

From Spry Wiki

Jump to: navigation, search

This is a very simple script I learned years ago that has helped me recover from more than on disaster. You run the command /bin/v or 'v' instead of using 'vi' or 'vim'. The script 'v' simply copies the file you are editing with a timestamp to the /tmp directory before you make any changes. This allows you to have multiple unique copies of a file saved in /tmp so you can revert back to a previous version on the fly if needed.

All you have to do to create the script is type 'vi /bin/v' then cut and paste the contents of the script below and save the file (:wq).

#!/bin/sh
FILE="${1}"
[ -f "${FILE}" ] && {
        COPY="/tmp/`basename ${FILE}`_`date '+%H%M%S'`"
        cp ${FILE} ${COPY} || {
                echo -e "\007Error: unable to save copy of ${FILE}"
                exit 1
        }
}
/bin/vi ${FILE}

Now 'chmod +x /bin/v' and your done. Next time you want to edit a text file use v instead of vi! Look in the /tmp directory and you will see something similar to "filename_204231". That is filename and the time of day 8:42:42.

Recent Changes | RSS Feed RSS