Question & Answer
Question
I have a tar archive that was created using absolute paths to the files. How can I restore those files to a different directory using relative paths?
Answer
The tar utility in AIX has no ability to alter the path of a file it will be restoring. If the archive was backed up with absolute paths, the files will restore using those paths. This has the potential to overwrite any files already in those locations.
For example viewing the archive below we can see it will overwrite existing system files:
$ tar tf myfiles.tar
/etc/filesystems
/etc/rc
The pax command can restore files backed up in a tar archive. Further, pax has a substitution function that may be used to alter the path of any files restored. The syntax is: -s /old/new/[gp] The substitution for "old" may be a regular expression.
An easy way to use this is to substitute the leading slash in the absolute path with a dot-slash. This will root the path in the current working directory.
$ pax -r -v -f myfiles.tar -s "/^\//.\//"
USTAR format archive
./etc/filesystems
./etc/rc
If we take apart the substitution string /^\//.\// we can see how this works.
Search for the old path delimiter:
/^\/
The first slash / is the beginning delimiter of the substitution pattern "old".
The circumflex character ^ is used to anchor the pattern to the beginning of any path in the archive.
The backslash \ tells the shell to not interpret the next character following it.
The foreslash / is the character we are searching for.
So this expression searches for any path with a leading / character, which would root it at the top of the filesystem tree.
Replace with the new path delimiter:
/.\/
The first slash / is the beginning delimiter of the substitution pattern "new".
The dot . is to be added to the path found in "old".
The backslash \ tells the shell not to interpret the next character following it.
The foreslash / is the next character to be substituted.
/
This foreslash ends the string.
In summary, this substitution will find any path beginning with a leading slash / and substitute a dot-slash ./ for that. This has the effect of restoring the files using their same path elements, but starting in your current working directory. In this way you can avoid overwriting existing files on the system you are restoring the files from.
Was this topic helpful?
Document Information
Modified date:
17 June 2018
UID
isg3T1019835