Find the answer to your Linux question:
Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Irithori
I am writing a script that installs updates to an embedded system. The script is required to extract a update tarball compressed with either .gz .bz2 or .xz. I have ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! RobertF's Avatar
    Join Date
    Feb 2008
    Location
    Cape Town
    Posts
    19

    Extracting tarball without specifying compression format.


    I am writing a script that installs updates to an embedded system.

    The script is required to extract a update tarball compressed with either .gz .bz2 or .xz.

    I have noticed on my gentoo system that I am able to decompress .tar.gz, .tar.bz2 and tar.xz compressed tarballs with only the -xvf option (ie without specifying the the compression filter -z -j or J options)

    My question is: Can this behavior be expected for all versions of tar and will it be safe to assume this behavior if using the format "tar -xvf {*.tar.gz,*.tar.bz2,*.tar.xz}" in my script.

  2. #2
    Just Joined! RobertF's Avatar
    Join Date
    Feb 2008
    Location
    Cape Town
    Posts
    19
    UPDATE:
    .tar.gz and .tar.bz2 can be extracted using only the -xvf tar options. However tar.xz requires the -J option included otherwise tarball will not be extracted. Thus I will need to check the extension and use the compression filter option in my script.

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,391
    May I suggest to rely on file, rather than a filename suffix?

    e.g.:
    Code:
    file -i *.tar.*
    filename1.tar.gz:    application/x-gzip; charset=binary
    filename2.tar.bz2:  application/x-bzip2; charset=binary
    filename3.tar.xz:    application/x-xz; charset=binary
    Should be more accurate. Suffix checking can be an additional check, though.
    RobertF likes this.
    You must always face the curtain with a bow.

  4. #4
    Just Joined! RobertF's Avatar
    Join Date
    Feb 2008
    Location
    Cape Town
    Posts
    19
    Good plan. Thanks

    Just a note on file though: file will determine that the file is either a gzip, bzip2 or a xz type file but won't tell you if it is a tar file unless you have already extracted the tar from the gzip, bzip2 or a xz type file.

    In my script I was hoping to extract the whole tarball with only the tar command. If I could extract each type of compressed tarball with the simple command tar -xvf then I could cut down on a bit of logic. However tar.xz type tarballs require the compression filter option. No problem though. Will just add some checking logic to my script.

    However, I strongly agree with you in rather using file types as outputted by the file command rather than relying on the file name wherever possible.
    Last edited by RobertF; 12-06-2011 at 10:59 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •