PRP PRP - 1 year ago 130
Linux Question

Structure of .tar files

I am trying to run some benchmarks which take in input a tar file.

Now there is a inside the tar file and that needs to be modified and the folder has to be made a .tar again.

The original benchmark works, but the modifies one doesn't. I believe that it is a problem with the file format.

Note : My modification is not creating the problem. If I just uncompress the working tar and tar it again without modification, it does not work. Surprisingly the size of the new file changes.

What I tried :

  1. file
    command on the working and non-working tar files.
    Both returned same
    POSIX tar archive

  2. Tried to run the command
    tar cvf folder_name.tar folder_name/

    Does not work.

What works :

I am on Ubuntu (14.04) and I double clicked on the tar, directly edited the file I wanted and updated it. This works, but is not a feasible solution as I have a large number of files and I want to write a script to automate it.

Screenshot of how it works with GUI :

enter image description here

Answer Source

Does the original tar file include the top-level directory name? It doesn't look like it from your screenshot. If you re-create the tar file with a top level directory, as indicated by point 2 in the things you tried, the structure won't be the same, and whatever program is trying to consume the tar file won't be able to parse it.

How do you test "If I just uncompress the working tar and tar it again without modification, it does not work." In a GUI or in a shell? If in a shell - what exact commands do you use?

In a shell, you can get the contents of the tarball with the command tar -tf filename.tar. If all the files it lists starts with the same folder name, your tarball includes a top level directory. If it just lists various files and subdirectories, it doesn't. (Tarballs that don't are an abomination, but if whatever you are using them for requires it, you'll just have to cope.)

I'm guessing that if you do this on your original tar file and your modified, non-working tar file, the results will differ.

The following should work in a shell if you have/need a tarball without a toplevel directory:

$ mkdir workdir
$ cd workdir
$ tar -xf ../tarball.tar
<edit your file however you like>
$ tar -cf ../tarball-new.tar *
$ cd ..
$ rm -r workdir

In case you have/need a tarball with a toplevel directory, the following should suffice:

$ tar -xf ../tarball.tar
$ cd toplevel_directory
<edit your file however you like>
$ cd ..
$ tar -cf tarball-new.tar toplevel_directory
$ rm -r toplevel_directory

Edit: I'm glad it worked for you. The point is, of course, that tar includes the paths of the files it stores, not just the filenames. So if you need a flat list of files, you need to run tar in the directory containing those files, giving all of them as arguments to tar. If you try to take the shortcut of going up a level and only specifying the directory name to pack up, tar will include the directory name in the archive.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download