#!/bin/bash # archive.sh - Datestamp Files # Author: Sean B. Palmer, inamidst.com function doc() { echo "Usage: $0 [options] []" echo "Copies filename to basename-isotime.ext" echo " -h --help Print help information" echo " -m --modified Use mtime not current time" } case $1 in -h|--help) doc; exit;; -m|--modified) shift; DATE=$(stat -c %y $1 | cut -d' ' -f1);; *) DATE=$(date -u +%Y%m%d-%H%M%S);; esac # Check that the argument is a file if [[ ! -f $1 || -z $1 ]] then echo "Error: <$1> isn't a file" exit 1 fi # Get the filename, base, and extension FN=$(basename $1) BASE=${FN%%.*} case $FN in *.*) EXT=${FN#*.};; esac # Only decorate these vars if they're non-empty BASE=${BASE:+${BASE}-} EXT=${EXT:+.${EXT}} # Select the directory to copy to if [[ -n $2 && -d $2 ]] then DIR=${2%/}/ else DIR=${1%$FN} fi # Copy the file to the datestamped version TARGET=${DIR}${BASE}${DATE}${EXT} cp -ip $1 $TARGET && echo Copied to: $TARGET # [EOF]