Solved

tar - flatten on extract?

Posted on 2003-11-30
7
2,762 Views
Last Modified: 2012-06-27
Call me an old fool, but i can't seem to see an option for flattening the path on extract. i.e. to ignore the directory structure and just to put the file into the current directory. Can anyone enlighten me? I'm using

tar (GNU tar) 1.13.19
0
Comment
Question by:CEHJ
7 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9846423
which os u are using.

on OSF or digiatl Unix there is option
-P.

check guntar for this option.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 9846686
Using Linux. This is what it says about -P (not sure if i know what they mean)

  -P, --absolute-names         don't strip leading `/'s from file names
0
 
LVL 1

Accepted Solution

by:
mbekker earned 125 total points
ID: 9848896
Hi CEHJ,

As far as I can see there is no option to flatten the path. (GNU tar 1.13.25)

You could try the following script:

tarfile=$1
shift
tar -tvf $tarfile | grep -v "^d" | awk {'print $6'} | while read filename
do
  tar "$@" -O -xf Desktop.tar $filename >${filename##*/}
done

The first parameter for this script would be the tar-archive, all other parameters will be given to the second tar command, which extracts the files.

Good luck!
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:mbekker
ID: 9848901
Bummer, got a typo in my script! ;-)

The "Desktop.tar" part must be: $tarfile
0
 
LVL 86

Author Comment

by:CEHJ
ID: 9849314
Hmm. Thanks. I can give that a try. I'm not sure what character ^d is and you lose me after that (don't know awk really, etc.) ;-)
0
 
LVL 1

Expert Comment

by:mbekker
ID: 9850338
The ^ in grep -v "^d"  means the beginning of a line. The first character in the line may not (-v) be a 'd'.

When listing a tar archive, the directories are also listed. They don't have to be extracted, so with a grep -v "^d" they will be excluded.

The 'awk' part will only print the sixth field every line, where the filename is stored. For every file the next tar command will be runned:

  tar "$@" -O -xf $tarfile $filename >${filename##*/}

where

 "$@"  all other arguments given to script will be passed here to tar command
 -O     write file to stdout
 -x      extract archive
 -f $tarfile   tar archive to extract
 $filename  complete path+file to extract
 >${filename##*/}  redirect output to filename without path
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9909254
How bad do you want this? If you are fluent in Perl, you can use the Archive:Tar package (http://www.perl.com/CPAN-local/modules/by-module/Archive/) to extract the files yourself and write them out in one directory. It's a bit more work, but if you have to do this not just once, it may be worth the time.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question