Prepend prefix in tar

The GNU version of tar supports the --transform option (and its alias --xform), you could use it like this

tar --transform "s/^$MYPATH/$VERSION/" -cf archive.tar.bz2 "$MYPATH"

For example, given this directory tree

foo
└── foo.txt

the command

tar --transform "s/^foo/bar/" -cf foo.tar.bz2 foo

will produce an archive like

$ tar -tf foo.tar.bz2
bar/
bar/foo.txt

To tar the current directory and add a prefix, this worked for me:

tar --transform 's,^\.,$VERSION,' -cf foo.tar .

Tags:

Bash

Tar