You're replying to a comment by Peteris Krumins.

June 02, 2012, 20:32

Here is a function called bk that does it. It works with files both of form file.ext and also files of form file (without ext). It also checks if the destination file doesn't exist. If it does, it adds _bk until such file doesn't exist. After it's copied the file it opens the original in vim.

function bk {
 if [[ -z $1 ]]; then
    echo "Usage: bk <file>"
    return
 fi

 file=$(basename $1)
 bk=_bk

 while :; do
  case $file in
   *.*)
    newfile=$(echo $file | sed 's/\(.*\)\.\(.*\)/\1'$bk'.\2/')
    ;;
   *)
    newfile=${file}$bk
    ;;
  esac
  if [[ ! -e $newfile ]]; then
   break
  fi
  bk=${bk}_bk
 done

 cp "$1" "$(dirname $1)/$newfile"
 if [[ $? -eq 0 ]]; then
    vim "$1"
 fi
}

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "cdrom_325": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.