On Tue, 03 Apr 2007 11:21, Christopher Sawtell wrote:
> On Tuesday 03 April 2007, Ross Drummond wrote:
> > How do I extract a named file from a tar archive and place it in the
> > current directory without the file path appended to the file.?
>
> man tar he say:-
>
>  --strip-components NUMBER, --strip-path NUMBER
>    strip NUMBER of leading components from file names
>          before extraction
>
> (1) tar-1.14 uses --strip-path,
>     tar-1.14.90+ uses  --strip-components
>
> e.g.
> [EMAIL PROTECTED] ~/tmp $ mkdir -p one/two/three/
> [EMAIL PROTECTED] ~/tmp $ touch one/two/three/file.ext
> [EMAIL PROTECTED] ~/tmp $ tar cvf example.tar one
> one/
> one/two/
> one/two/three/
> one/two/three/file.ext
>
> [EMAIL PROTECTED] ~/tmp $ tar tvf example.tar one
> drwxr-xr-x chris/users       0 2007-04-03 11:13 one/
> drwxr-xr-x chris/users       0 2007-04-03 11:13 one/two/
> drwxr-xr-x chris/users       0 2007-04-03 11:14 one/two/three/
> -rw-r--r-- chris/users       0 2007-04-03 11:14 one/two/three/file.ext
>
> [EMAIL PROTECTED] ~/tmp $ tar xv --strip-components 3 -f example.tar \
> one/two/three/file.ext
>
> [EMAIL PROTECTED] ~/tmp $ ls -l
> total 16
> -rw-r--r-- 1 chris users 10240 Apr  3 11:14 example.tar
> -rw-r--r-- 1 chris users     0 Apr  3 11:14 file.ext
>
> --
> CS

Thank you Chris and Nick. Here is another way to skin this cat,

Extract the contents of the target file to standard output using the -O 
(--to-stdout) argument and redirect this output to a new file using the 
redirection operator.

I have changed the example above to illustrate.

[EMAIL PROTECTED] ~/tmp $ mkdir -p one/two/three/
[EMAIL PROTECTED] ~/tmp $ echo "bla bla bla"  >one/two/three/file.ext
[EMAIL PROTECTED] ~/tmp $ tar cvf example.tar one
one/
one/two/
one/two/three/
one/two/three/file.ext

[EMAIL PROTECTED] ~/tmp $ tar tvf example.tar one
drwxr-xr-x chris/users       0 2007-04-03 11:13 one/
drwxr-xr-x chris/users       0 2007-04-03 11:13 one/two/
drwxr-xr-x chris/users       0 2007-04-03 11:14 one/two/three/
-rw-r--r-- chris/users       0 2007-04-03 11:14 one/two/three/file.ext

[EMAIL PROTECTED] ~/tmp $ tar xOf example.tar \
one/two/three/file.ext  >newfile

[EMAIL PROTECTED] ~/tmp $ ls -l
total 20
-rw-r--r-- 1 chris users 10240 Apr  3 11:14 example.tar
-rw-r--r-- 1 chris users    12 Apr  3 11:14 newfile

[EMAIL PROTECTED] ~/tmp $ cat newfile
bla bla bla

Cheers Ross Drummond

Reply via email to