6.7 Modifying File and Member Names

Tar archives contain detailed information about files stored
in them and full file names are part of that information. When
storing a file to an archive, its file name is recorded in it,
along with the actual file contents. When restoring from an archive,
a file is created on disk with exactly the same name as that stored
in the archive. In the majority of cases this is the desired behavior
of a file archiver. However, there are some cases when it is not.

First of all, it is often unsafe to extract archive members with
absolute file names or those that begin with a ‘../’. GNUtar
takes special precautions when extracting such names and provides a
special option for handling them, which is described in
Absolute File Names.

Secondly, you may wish to extract file names without some leading
directory components, or with otherwise modified names. In other
cases it is desirable to store files under differing names in the
archive.

GNUtar provides several options for these needs.

‘--strip-components=number’

Strip given number of leading components from file names before
extraction.

For example, suppose you have archived whole ‘/usr’ hierarchy to
a tar archive named ‘usr.tar’. Among other files, this archive
contains ‘usr/include/stdlib.h’, which you wish to extract to
the current working directory. To do so, you type:

$ tar -xf usr.tar --strip=2 usr/include/stdlib.h

The option ‘--strip=2’ instructs tar to strip the
two leading components (‘usr/’ and ‘include/’) off the file
name.

If you add the ‘--verbose’ (‘-v’) option to the invocation
above, you will note that the verbose listing still contains the
full file name, with the two removed components still in place. This
can be inconvenient, so tar provides a special option for
altering this behavior:

‘--show-transformed-names’

Display file or member names with all requested transformations
applied.

Note: the POSIX standard does not specify what should happen
when you mix the ‘g’ and number modifiers. GNUtar
follows the GNU sed implementation in this regard, so
the interaction is defined to be: ignore matches before the
numberth, and then match and replace all matches from the
numberth on.

In addition, several transformation scope flags are supported,
that control to what files transformations apply. These are:

‘r’

Apply transformation to regular archive members.

‘R’

Do not apply transformation to regular archive members.

‘s’

Apply transformation to symbolic link targets.

‘S’

Do not apply transformation to symbolic link targets.

‘h’

Apply transformation to hard link targets.

‘H’

Do not apply transformation to hard link targets.

Default is ‘rsh’, which means to apply tranformations to both archive
members and targets of symbolic and hard links.

Default scope flags can also be changed using ‘flags=’ statement
in the transform expression. The flags set this way remain in force
until next ‘flags=’ statement or end of expression, whichever
occurs first. For example:

--transform 'flags=S;s|^|/usr/local/|'

Here are several examples of ‘--transform’ usage:

Extract ‘usr/’ hierarchy into ‘usr/local/’:

$ tar --transform='s,usr/,usr/local/,' -x -f arch.tar

Strip two leading directory components (equivalent to
‘--strip-components=2’):

$ tar --transform='s,/*[^/]*/[^/]*/,,' -x -f arch.tar

Convert each file name to lower case:

$ tar --transform 's/.*/\L&/' -x -f arch.tar

Prepend ‘/prefix/’ to each file name:

$ tar --transform 's,^,/prefix/,' -x -f arch.tar

Archive the ‘/lib’ directory, prepending ‘/usr/local’
to each archive member:

$ tar --transform 's,^,/usr/local/,S' -c -f arch.tar /lib

Notice the use of flags in the last example. The ‘/lib’
directory often contains many symbolic links to files within it.
It may look, for example, like this:

Unlike ‘--strip-components’, ‘--transform’ can be used
in any GNUtar operation mode. For example, the following command
adds files to the archive while replacing the leading ‘usr/’
component with ‘var/’:

$ tar -cf arch.tar --transform='s,^usr/,var/,' /

To test ‘--transform’ effect we suggest using
‘--show-transformed-names’ option:

If both ‘--strip-components’ and ‘--transform’ are used
together, then ‘--transform’ is applied first, and the required
number of components is then stripped from its result.

You can use as many ‘--transform’ options in a single command
line as you want. The specified expressions will then be applied in
order of their appearance. For example, the following two invocations
are equivalent: