Solved

tar - flatten on extract?

Posted on 2003-11-30
7
2,753 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

776 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