-   Linux - Software (
-   -   Problem with properly creating a tar archive (

FransE 02-28-2004 11:15 AM

Problem with properly creating a tar archive
Hi all,

In a bash script of mine I create a tar archive from a file which contains
absolute paths to the files the archive should contain(supplied via the
--files-from argument). The lines is of the type /foo/bar/a, /foo/bar/b etc.
- all the specified files is in one folder.
The problem is, when I untar the archive the whole directory structure is
created, foo/bar/ and then the files - I just want all the files in the
toplevel of the archive. Any ideas? Suggestions highly appreciated.

BTW, does anyone know a good mailinglist/forum for tricky hardware questions?
(I don't want to go for LKML)



TheOneKEA 02-28-2004 11:21 AM

You want a flat tar archive, right - one where the directory structure does not exist?

FransE 02-28-2004 11:26 AM

Yupp! :)


TheOneKEA 02-28-2004 11:33 AM

What you want doesn't exist - the only workaround is to create the tarball with the -P switch to prevent the stripping of leading '/' characters, and then unnzipping the tarball in the parent directory of the directory structure that you want backed up.

FransE 02-28-2004 11:39 AM

Ok. But there must be a solution to my problem:

I have N files spread out over the place, and I want to collect them together in the toplevel of the tar file. (and the files are in different places, some are in /foo/bar/ othere's in /foo/foo)


dford 02-28-2004 03:15 PM

There is indeed a way. You will probably have to do some scripting, but it isn't that hard.

Lets start with a file, file.lst, that contains all of the fully qualified file names you want to tar:



TARFILE=/backups/backup_$(date +%G%m%d).tar
for i in $(cat file.lst)
  DIRECTORY=$(dirname $i)
  FILEN=$(basename $i)
  if [ -f $TARFILE ]
        (cd $DIRECTORY ; tar rvf $TARFILE $FILEN)
        (cd $DIRECTORY ; tar cvf $TARFILE $FILEN)

Something like this should do it.

The tar cvf creates the tar file tar rvf appends to that tar file. Doing the cd inside of parens spawns a separate shell and when it exits you are back in the same place.

Hope this helps!

All times are GMT -5. The time now is 02:27 AM.