[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 file to an archive, its file name is recorded in the archive 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 ‘../’. GNU tar 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.

GNU tar provides two 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 to the above invocation ‘--verbose’ (‘-v’) option, 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.

For example:

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

Notice that in both cases the file is ‘stdlib.h’ extracted to the current working directory, ‘--show-transformed-names’ affects only the way its name is displayed.

This option is especially useful for verifying whether the invocation will have the desired effect. Thus, before running

 
$ tar -x --strip=n

it is often advisable to run

 
$ tar -t -v --show-transformed --strip=n

to make sure the command will produce the intended results.

In case you need to apply more complex modifications to the file name, GNU tar provides a general-purpose transformation option:

--transform=expression

Modify file names using supplied expression.

The expression is a sed-like replace expression of the form:

 
s/regexp/replace/[flags]

where regexp is a regular expression, replace is a replacement for each file name part that matches regexp. Both regexp and replace are described in detail in The "s" Command: (sed)The "s" Command section `The `s' Command' in GNU sed.

As in sed, you can give several replace expressions, separated by a semicolon.

Supported flags are:

g

Apply the replacement to all matches to the regexp, not just the first.

i

Use case-insensitive matching

x

regexp is an extended regular expression (see Extended regular expressions: (sed)Extended regexps section `Extended regular expressions' in GNU sed).

number

Only replace the numberth match of the regexp.

Note: the posix standard does not specify what should happen when you mix the ‘g’ and number modifiers. GNU tar 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.

Any delimiter can be used in lieue 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/\//-/.

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

  1. Extract ‘usr/’ hierarchy into ‘usr/local/’:
     
    $ tar --transform='s,usr/,usr/local/,' -x -f arch.tar
    
  2. Strip two leading directory components (equivalent to ‘--strip-components=2’):
     
    $ tar --transform='s,/*[^/]*/[^/]*/,,' -x -f arch.tar
    
  3. Prepend ‘/prefix/’ to each file name:
     
    $ tar --transform 's,^,/prefix/,' -x -f arch.tar
    
  4. Convert each file name to lower case:
     
    $ tar --transform 's/.*/\L&/' -x -f arch.tar
    

Unlike ‘--strip-components’, ‘--transform’ can be used in any GNU tar 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:

 
$ tar -cf arch.tar --transform='s,^usr/,var/,' \
       --verbose --show-transformed-names /

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:

 
$ tar -cf arch.tar --transform='s,/usr/var,/var/' \
                        --transform='s,/usr/local,/usr/,'
$ tar -cf arch.tar \
               --transform='s,/usr/var,/var/;s,/usr/local,/usr/,'

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Sergey Poznyakoff on April, 14 2008 using texi2html 1.78.