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

B.2 Restoring Intermediate Directories

A common concern is how to extract permissions and ownerships of intermediate directories when extracting only selected members from the archive. To illustrate this, consider the following archive:

# tar tvf A.tar
drwxr-xr-x root/root         0 2017-11-16 14:39 foo/
dr-xr-x--- gray/user         0 2017-11-16 14:39 foo/bar/
-rw-r--r-- gray/user        10 2017-11-16 14:40 foo/bar/file

Suppose you extract only the file ‘foo/bar/file’, while being ‘root’:

# tar xvf A.tar foo/bar/file
foo/bar/file

Now, let’s inspect the content of the created directories:

# find foo -ls
427257    0 drwxr-xr-x   3 root     root    16 Nov 17 16:10 foo
427258    0 drwxr-xr-x   2 root     root    17 Nov 17 16:10 foo/bar
427259    0 -rw-r--r--   1 gray     user    10 Nov  6 14:40 foo/bar/file

The requested file is restored, including its ownership and permissions. The intermediate directories, however, are created with the default permissions, current timestamp and owned by the current user. This is because by the time tar has reached the requested file, it had already skipped the entries for its parent directories, so it has no iformation about their ownership and modes.

To restore meta information about the intermediate directories, you’ll need to specify them explicitly in the command line and use the ‘--no-recursive’ option (see section Descending into Directories) to avoid extracting their content.

To automate this process, Neal P. Murphy proposed the following shell script(32):

#! /bin/sh
(while read path
 do
   path=`dirname $path`
   while [ -n "$path" -a "$path" != "." ]
   do
     echo $path
     path=`dirname $path`
   done
 done < $2 | sort | uniq) |
 tar -x --no-recursion -v -f $1 -T - -T $2

The script takes two arguments: the name of the archive file, and the name of the file list file.

To complete our example, the file list will contain single line:

foo/bar/file

Supposing its name is ‘file.list’ and the script is named ‘restore.sh’, you can invoke it as follows:

# sh restore.sh A.tar file.list

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

This document was generated on August 23, 2023 using texi2html 5.0.