Sample Header Ad - 728x90

GNU tar's "transform" option behaves inconsistently when pattern ends in "/"

0 votes
1 answer
493 views
I have come across the following strange behavior of GNU tar when using the --transform option to transform path elements: When I try to tar an entire sub-directory and want to transform the path to this directory, the transformation is _not_ applied to the directory itself, but only to its content, when the transformation pattern explicitly contains the /. To reproduce: * create a directory test-dir with dummy content:
$ mkdir test-dir
    $ touch test-dir/test{1..50}.txt
* tar this directory with renaming of test-dir/ to transformed-dir/, and instruct tar to print the transformed names for checking:
$ tar --transform="s,^test-dir/,transformed-dir/," --show-transformed-names -cvf test.tar test-dir
    test-dir/
    transformed-dir/test25.txt
    transformed-dir/test29.txt
    transformed-dir/test47.txt
    ...
As you can see, the directory itself is _not_ renamed correctly, although the renaming works for all files within the directory. * For comparison, use the same transformation but _without_ the trailing /:
$ tar --transform="s,^test-dir,transformed-dir," --show-transformed-names -cvf test2.tar test-dir
    transformed-dir/
    transformed-dir/test25.txt
    transformed-dir/test29.txt
    transformed-dir/test47.txt
    ...
Now, the directory itself _is_ correctly renamed. The behavior doesn't change when the ^ anchor is omitted, and is independent on whether the directory to be tarred is specified with or without trailing / on the command line. * I wondered if the problem was that when the / is specified, in case of the directory the _entire_ filename is subject to replacement. However, when specifying a transformation that would rename an entire file, that works correctly:
$ tar --transform="s,^test-dir/test29.txt,transformed-dir/file.txt," --show-transformed-names -cvf test3.tar test-dir
    test-dir/
    test-dir/test25.txt
    transformed-dir/file.txt
    test-dir/test47.txt
    ...
So it really seems that the trailing / is the problem. Is this a feature, a bug, or did I somehow misunderstand the scope/syntax of the option? The tar version is GNU tar 1.28.
Asked by AdminBee (23588 rep)
Jan 19, 2021, 09:50 AM
Last activity: Nov 2, 2022, 03:44 PM