LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Problem with properly creating a tar archive (https://www.linuxquestions.org/questions/linux-software-2/problem-with-properly-creating-a-tar-archive-151568/)

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)


Cheers,

________________Frans

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! :)

Frans

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)

Cheers,
Frans

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

/var/log/dmesg
/usr/bin/firebird

Code:

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

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 01:56 PM.