Solved

tar - flatten on extract?

Posted on 2003-11-30
7
2,741 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now