#!/bin/bash # move one or more files/dirs to date-tagged names: # foo --> foo-2001.03.29 # where the date part is the date of the last file modification # ... online at http://www.jjj.de/ # author: Joerg Arndt ( arndt (AT) jjj.de ) # version: 2003-January-04 (15:39) THIS='jjold'; # `basename $0`; function usage { cat < foo-2001.03.29 where the date part is the date of the last file modification OPTION(s) must precede any other arguments. "-q" Quiet: supress output (option is unset if "-n" is also given). "-n" sets dry run mode (do Nothing) "-t" append date and Time "-w" noW: use current date/time "-c" Copy file (instead of moving it) "-d " move/copy file to Directory "-z" compress file after renaming (using gZip) NOTE: -z presently doesn't loop until a uniq name is found $THIS is online: at http://www.jjj.de/ EOF } if [ "$1" = "--help" ]; then usage; exit 0; fi if [ "$1" = "-help" ]; then usage; exit 0; fi if [ -z "$*" ] ; then usage; exit 1; fi DRYRUN= COPY= TODIR= TIME= QUIET= COMPRESS= NOW= while getopts :ctnwqhz\?d: OPT; do case $OPT in h|+h|\?|+\?) usage exit 0; ;; n|+n) DRYRUN=1; ;; c|+c) COPY=1; ;; d|+d) TODIR=$OPTARG; ;; t|+t) TIME=1; ;; q|+q) QUIET=1; ;; z|+z) COMPRESS=1; ;; w|+w) NOW=1; ;; *) # echo "usage: ${0##*/} [+- ARG] [+-ct] [--] ARGS..." usage; exit 1; esac done shift $[ OPTIND - 1 ] #echo "DRYRUN=$DRYRUN COPY=$COPY TIME=$TIME" #exit; if [ -n "DRYRUN" ]; then QUIET=""; fi if [ -n "$TODIR" ]; then test -d $TODIR -a -w $TODIR || \ { echo "$TODIR must be a writable directory."; exit 1; }; TODIR=$TODIR'/'; fi for f in $* ; do f=${f%%/} if [ ! -e $f ]; then echo "$f: no such file or directory" continue; ## we might want a nonzero return value fi if [ -n "$TIME" ]; then FORMAT='%Y.%m.%d-%H:%M:%S'; ## exact else FORMAT='%Y.%m.%d'; ## just date fi if [ -n "$NOW" ]; then DATE=$(date +$FORMAT); ## date = now else DATE=$(date -r $f +$FORMAT); ## date = file modification time fi NEW=$f-$DATE; if [ -e $TODIR$NEW ] ; then ## loop until a unused filename is found NUM=1; while [ -e $TODIR$NEW ] ; do NEW=$f-$DATE--$NUM let NUM=$NUM+1 done fi DNEW=$TODIR$NEW; if [ -z "$QUIET" ]; then echo "$f --> $DNEW"; fi if [ -n "$DRYRUN" ]; then continue; fi ## dryrun: just pretend actions if [ -n "$COPY" ]; then cp -a $f $DNEW else mv $f $DNEW fi ## FIXME: need to find unused name.gz / name.tar.gz if [ -n "$COMPRESS" ]; then if [ -d "$NEW" ]; then ZNEW=$DNEW.tar.gz tar cfz $ZNEW $DNEW rm -rf $DNEW else ZNEW=$DNEW.gz gzip -9 $DNEW fi if [ -z "$QUIET" ]; then echo " --> $ZNEW"; fi fi done exit 0; #######################################