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.

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)

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

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:

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

you can do

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.

--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

 pkgutil -yi gtar

GNU tar has the option --transform , but I don't fully understand how it works.
May be something like this:

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

Right, as I said, gtar.

gtar is also part of Solaris 10 as /usr/sfw/bin/gtar.

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

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

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

Unfortunately, the directories in the tar file get restored this way too.

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?

Assuming no embedded newlines in the filenames:

find . -type f | 
  xargs -I{}  cp -- {} <dest_dir>

---------- Post updated at 02:51 PM ---------- Previous update was at 02:49 PM ----------

Yep,
or even shorter:

's .*/  '

ok - the cp script works. Job done. Cheers all!

I'm a little late on that one but for the record Solaris has been including pax for ages. This archiver handles tar files and has a substitution option too:
eg:

pax -r -s '/.*\///' -f file.tar
1 Like