[plug] Adjust a timestamp by a specified amount

James Devenish devenish at guild.uwa.edu.au
Sun Nov 9 12:29:05 WST 2003


In message <1068346073.1771.51.camel at latte.internal.itmaze.com.au>
on Sun, Nov 09, 2003 at 01:17:53PM +1030, Onno Benschop wrote:
> Before I start writing code, anyone know of a way of changing a file's
> time stamp by a set amount?

Someone will surely recommend Perl, but Perl hurts my C brain so I'll
give you zsh instead. The following script will compare the timestamps
of two files and adjust all subsequent files accordingly. (It is
complicated by the fact that `touch` does not accept dates in seconds
since UNIX Epoch.)

zmodload zsh/stat
function restamp {
    if [[ $#* -lt 3 ]]; then
        echo Usage: restamp correct incorrect "[other ...]"
        echo Example: restamp ../camera1/image001 ./image001 "*(.)"
        echo
        echo The mtimes of "'correct'" and "'incorrect'" will be compared and the
        echo difference recorded. All "'other'" files will then have their
        echo mtimes adjusted by that difference.
    else
        OFFSET=$((`stat +mtime $1` - `stat +mtime $2`))
        shift 2
        for f in $*; do
            OLDTIME=`stat -s +mtime $f`
            if [[ $OFFSET -lt 0 ]]; then
                touch -d "$OLDTIME $((-OFFSET)) seconds ago" $f
            else
                touch -d "$OLDTIME $OFFSET seconds" $f
            fi
        done
    fi
}


_______________________________________________
plug mailing list
plug at plug.linux.org.au
http://mail.plug.linux.org.au/cgi-bin/mailman/listinfo/plug


More information about the plug mailing list