Sunday, September 9, 2007

Tip for the day: SAVE A COPY

Ever edited a file, then wished you'd saved a copy first?

This script will save it for you. It even handles multiple files, and multiple edits of the same file.

############### Cut here ##########
#!/bin/ksh
# My vi : Save a backup of the file(s) to edit
for file in $* ; do
if [[ -a $file ]] ; then
basefile=`basename $file`
cp $file ~/bak/${basefile}.`date +%C%y%m%d%H%M%S`
fi
done
/usr/bin/vi $*
############### Cut here ##########

You'll need to create a directory called 'bak' in your $HOME directory, and it would be a good idea to set up a cron job to clear out old copies periodically.

No comments: