#!/bin/bash # # Rewrite a tar file with the time and owner from a reference file. # # Copyright (C) 2014 Ben Asselstine # This program is licensed under the terms of the GNU General Public License, # version 3, or at your option any later version. http://gnu.org/l/gpl-3.0.txt # # Caveats: # # This program only works with .tar.gz, .tgz, .tar.bz, and .tar.xz files. # # Only the first top-level directory in the archive has times modified # the old .tar file is kept in a .old backup file # # Req'd programs: coreutils (ls, mv, rm, head, mktemp, chown, touch), find, tar # # do we have the command line arguments? if [ ! -f $1 ]; then echo $0: file.tar.gz reference-file exit 1 fi if [ ! -f $2 ]; then echo $0: file.tar.gz reference-file exit 1 fi # do we support the compression method of the tar file? z="" if [[ $1 = *.tar.gz || $1 = *.tgz ]] ; then z="z" elif [[ $1 = *.tar.bz ]] ; then z="j" elif [[ $1 = *.tar.xz ]] ; then z="J" else echo $0: file.tar.gz reference-file exit 1 fi # take the owner and group from the reference file owner=`stat -c %U $2` group=`stat -c %G $2` # extract the tar file into a temporary directory dir=`mktemp -d /tmp/rewrite-tar.XXXXXX` tar --directory=$dir -${z}xvf $1 if [ "$?" != "0" ]; then echo $0: failed to extract $1 to $dir exit 1 fi # touch each of the files and directories in the first directory of the archive extracteddir=`ls $dir | head -n1` find $dir/$extracteddir -exec touch --reference=$2 {} \; # keep a backup of the old tar file mv $1 $1.old # recreate the tar file tar --directory=$dir --owner=$owner --group=$group -${z}cvf $1 $extracteddir chown $owner:$group $1 touch --reference=$2 $1 rm -rf $dir exit 0