Attachment 'rewrite_faketime_tar_dates_script.txt'
Download 1 #!/bin/bash
2 #
3 # Rewrite a tar file with the time and owner from a reference file.
4 #
5 # Copyright (C) 2014 Ben Asselstine
6 # This program is licensed under the terms of the GNU General Public License,
7 # version 3, or at your option any later version. http://gnu.org/l/gpl-3.0.txt
8 #
9 # Caveats:
10 #
11 # This program only works with .tar.gz, .tgz, .tar.bz, and .tar.xz files.
12 #
13 # Only the first top-level directory in the archive has times modified
14 # the old .tar file is kept in a .old backup file
15 #
16 # Req'd programs: coreutils (ls, mv, rm, head, mktemp, chown, touch), find, tar
17 #
18
19 # do we have the command line arguments?
20 if [ ! -f $1 ]; then
21 echo $0: file.tar.gz reference-file
22 exit 1
23 fi
24
25 if [ ! -f $2 ]; then
26 echo $0: file.tar.gz reference-file
27 exit 1
28 fi
29
30 # do we support the compression method of the tar file?
31 z=""
32 if [[ $1 = *.tar.gz || $1 = *.tgz ]] ; then
33 z="z"
34 elif [[ $1 = *.tar.bz ]] ; then
35 z="j"
36 elif [[ $1 = *.tar.xz ]] ; then
37 z="J"
38 else
39 echo $0: file.tar.gz reference-file
40 exit 1
41 fi
42
43 # take the owner and group from the reference file
44 owner=`stat -c %U $2`
45 group=`stat -c %G $2`
46
47 # extract the tar file into a temporary directory
48 dir=`mktemp -d /tmp/rewrite-tar.XXXXXX`
49 tar --directory=$dir -${z}xvf $1
50 if [ "$?" != "0" ]; then
51 echo $0: failed to extract $1 to $dir
52 exit 1
53 fi
54
55 # touch each of the files and directories in the first directory of the archive
56 extracteddir=`ls $dir | head -n1`
57 find $dir/$extracteddir -exec touch --reference=$2 {} \;
58
59 # keep a backup of the old tar file
60 mv $1 $1.old
61
62 # recreate the tar file
63 tar --directory=$dir --owner=$owner --group=$group -${z}cvf $1 $extracteddir
64
65 chown $owner:$group $1
66 touch --reference=$2 $1
67
68 rm -rf $dir
69 exit 0
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.