Solved

tar - flatten on extract?

Posted on 2003-11-30
7
2,730 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Configuring EMC Clariion on Solaris 10 5 67
FTP on FreeBSD server 2 92
Solaris 10.  Nmap installation fails 2 40
MarkLogic 1 50
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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 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 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…

759 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

24 Experts available now in Live!

Get 1:1 Help Now