Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2852
  • Last Modified:

tar - flatten on extract?

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
CEHJ
Asked:
CEHJ
1 Solution
 
shivsaCommented:
which os u are using.

on OSF or digiatl Unix there is option
-P.

check guntar for this option.
0
 
CEHJAuthor Commented:
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
 
mbekkerCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mbekkerCommented:
Bummer, got a typo in my script! ;-)

The "Desktop.tar" part must be: $tarfile
0
 
CEHJAuthor Commented:
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
 
mbekkerCommented:
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
 
Karl Heinz KremerCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now