2011年1月26日 星期三

tar: --xform

Using --xform to add prefix folder to the files.

Init test:

$ rm test abc* -rf;
$ mkdir test/1 -p;
$ touch test/1/1;
$ ln -s 1 test/1/2;



$ tar c -C test 1 --xform 's/^/abc\//' | tar xv
abc\\/1/
abc\\/1/1
abc\\/1/2
There are additional // exist which we don't want.

Use , as the delimiter:
$ tar c -C test 1 --xform 's,^,abc/,' | tar xv
abc/1/
abc/1/1
abc/1/2
$ ls abc/1/ -al
total 8
drwxr-xr-x 2 test test 4096 2011-01-27 09:04 .
drwxr-xr-x 3 test test 4096 2011-01-27 09:06 ..
-rw-r--r-- 1 test test 0 2011-01-27 09:04 1
lrwxrwxrwx 1 test test 5 2011-01-27 09:06 2 -> abc/1
This is ok, but prefix abc is added to link as well, which is certainly not what we want

Add S:
$ tar c -C test 1 --xform 's,^,abc/,S' | tar xv
abc/1/
abc/1/1
abc/1/2
test@cavm-release:~$ ls abc/1/ -al
total 8
drwxr-xr-x 2 test test 4096 2011-01-27 09:04 .
drwxr-xr-x 3 test test 4096 2011-01-27 09:06 ..
-rw-r--r-- 1 test test 0 2011-01-27 09:04 1
lrwxrwxrwx 1 test test 1 2011-01-27 09:07 2 -> 1
This is what we want.

TAR: 6.7 Modifying File and Member Names
http://www.gnu.org/software/tar/manual/html_section/transform.html
Any delimiter can be used in lieu of ‘/’, the only requirement being that it be used consistently throughout the expression. For example, the following two expressions are equivalent:


s/one/two/
s,one,two,

Changing delimiters is often useful when the regex contains slashes. For example, it is more convenient to write s,/,-, than s/\//-/.

(.......................)

Using the expression ‘s,^,/usr/local/,’ would mean adding ‘/usr/local’ to both regular archive members and to link targets. In this case, ‘/lib/libc.so.6’ would become:


/usr/local/lib/libc.so.6 -> /usr/local/libc-2.3.2.so

This is definitely not desired. To avoid this, the ‘S’ flag is used, which excludes symbolic link targets from filename transformations

沒有留言: