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-dirwith dummy content:$ mkdir test-dir $ touch test-dir/test{1..50}.txt tarthis directory with renaming oftest-dir/totransformed-dir/, and instructtarto 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.