How to extract files from a tar file without creating the directories? | Unix Linux Forums | Solaris

  Go Back    


Solaris The Solaris Operating System, usually known simply as Solaris, is a Unix-based operating system introduced by Sun Microsystems. The Solaris OS is now owned by Oracle.

How to extract files from a tar file without creating the directories?

Solaris


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-08-2010
gkb gkb is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 8 October 2010, 9:57 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
How to extract files from a tar file without creating the directories?

Hello all.

I have a tar file that contains a number of files that are stored in different directories.

If I extract this tar file with -xvf , the directories get created.

Is there a way to extract all of the files into one directory without creating the directories stored in the tar file.
Sponsored Links
    #2  
Old 10-08-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 July 2014, 11:11 AM EDT
Location: NM
Posts: 10,160
Thanks: 269
Thanked 781 Times in 731 Posts
Not really - that is the builtin behavior of tar.

Try this on /var/tmp or /tmp or your home directory (be sure there is disk space beforehand)

Code:
cd /tmp
mkdir junk
cd junk
tar xvf mytarball.tar
cd ..
export DEST=/real/path/to/files
find junk -type -f  |
while read fname
do
     bn=$(basename $fname)
     mv $fname ${DEST}/${fn}
done 
rm -r ./junk

Sponsored Links
    #3  
Old 10-08-2010
hergp hergp is offline Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 26 July 2014, 3:41 PM EDT
Location: Vienna, Austria
Posts: 789
Thanks: 18
Thanked 167 Times in 149 Posts
If all the files are at the same tree depth (I mean, they all are in the same number of directories), you can use gtar with the --strip-path or --strip-components modifier.

Let's assume, your files pathnames are like these:

Code:
a/b/x
a/b/y
a/b/z
a/c/1
a/c/2
a/c/3

you can do


Code:
gtar xvf archive.tar --strip-components=2

and then you get all six files in the current directory.

If the tree depth is variable, I am not aware of any tar option you could use to extract the files the way you want.
    #4  
Old 10-08-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 July 2014, 11:11 AM EDT
Location: NM
Posts: 10,160
Thanks: 269
Thanked 781 Times in 731 Posts
--strip-components is not a Solaris tar option, it is for gnu tar.

You can get gtar at Blastwave.org - An OpenSolaris Community Site

You have to setup pkgutil first, then
Code:
 pkgutil -yi gtar

Sponsored Links
    #5  
Old 10-08-2010
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 1 August 2014, 5:31 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,666
Thanks: 182
Thanked 617 Times in 575 Posts
GNU tar has the option --transform , but I don't fully understand how it works.
May be something like this:


Code:
tar --transform 's#.*/\([^/]*\)$#\1#' -xvf <filename>

Sponsored Links
    #6  
Old 10-08-2010
hergp hergp is offline Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 26 July 2014, 3:41 PM EDT
Location: Vienna, Austria
Posts: 789
Thanks: 18
Thanked 167 Times in 149 Posts
Quote:
Originally Posted by jim mcnamara View Post
--strip-components is not a Solaris tar option, it is for gnu tar.
Right, as I said, gtar.
Quote:
Originally Posted by jim mcnamara View Post
gtar is also part of Solaris 10 as /usr/sfw/bin/gtar.

---------- Post updated at 14:03 ---------- Previous update was at 13:56 ----------

Quote:
Originally Posted by radoulov View Post
GNU tar has the option --transform , but I don't fully understand how it works.
May be something like this:


Code:
tar --transform 's#.*/\([^/]*\)$#\1#' -xvf <filename>

Ah, interesting new switch! OpenSolaris' gtar supports this option too, as I just found out. A shorter way would be


Code:
gtar --transform 's/.*\///' -xvf <filename>

Unfortunately, the directories in the tar file get restored this way too.
Sponsored Links
    #7  
Old 10-08-2010
gkb gkb is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 8 October 2010, 9:57 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for all of the replies.

Perhaps this problem can be approached from a different angle.

Is there a way to use the cp command on solaris to take all of the files in the directories, and to copy them all to one directory?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
creating separate directories according to file extension and keeping file in different directory as Deekay.p UNIX for Dummies Questions & Answers 2 07-28-2010 10:00 AM
Copying a files from a filter list and creating their associated parent directories hunter55 Shell Programming and Scripting 4 05-05-2010 04:22 PM
help needed with creating challenging bash script with creating directories I-1 Shell Programming and Scripting 7 04-29-2009 05:33 AM
Bash and Awk for creating directories and moving files Kiint Shell Programming and Scripting 11 07-24-2008 10:30 PM
extract tar files without creating directory here2learn UNIX for Dummies Questions & Answers 4 10-02-2006 07:42 PM



All times are GMT -4. The time now is 05:50 AM.