save (Source)

#!/bin/bash

# Moves filename.ext to filename.SAVE.ext, then copies filename.SAVE.ext to
# filename.ext. This unusual method preserves the inode number of the original
# file, for reasons that are purely aesthetic :)

# BUUS: This script is part of Brian's Useful Utilities Set

function die {
    echo "$1" >&2
    exit 1
}

# Do we have a valid filename?
[ -z "$1" ] && die "usage: save filename"


# Determine where the extension starts
ORIG_FN="$1"
POS=${#ORIG_FN}
while [ $POS -gt 0 -a "${ORIG_FN:POS:1}" != . ]
do
    POS=$((POS-1))
done
if [ $POS -gt 0 ]
then
    NAME="${ORIG_FN:0:$POS}"
    EXT="${ORIG_FN:$POS}"
else
    NAME="$ORIG_FN"
    EXT=''
fi

NEW_FN="$NAME.SAVE$EXT"
[ -f "$NEW_FN" ] && die "$NEW_FN already exists"

mv "$ORIG_FN" "$NEW_FN"
cp -a "$NEW_FN" "$ORIG_FN"

# vim: tabstop=4