How do I compress a directory into an archive file on AIX where tar z isn't available?

23,261

The correct command is

tar cvf - /path/dirtocompress | gzip > myarchive.tar.gz

This will compress the directory into one gzip file. As suggested by patrix, you can verify the paths are being stored correctly in this file using the following command which lists the contents of the file to stdout:

gunzip -c myarchive.tar.gz | tar -tvf -

The issue is that the Windows decompression software isn't interpreting/decompressing the archive correctly; WinZip 11.2 is ignoring the paths.

Using 7-zip for Windows, I was able to decompress the file and preserve the paths.

Share:
23,261
user5639117
Author by

user5639117

Updated on December 05, 2020

Comments

  • user5639117
    user5639117 over 3 years

    I have a directory on a Unix server that I need to compress (recursively, including all files and directories) into one archive file, that I can FTP to my Windows box. I need to keep the directory/file structure.

    All the help I can find says to use something like

    tar -cvfz myarchive.tar.gz /path/dirtocompress
    

    but I have no "z" option available - if I just type tar at the console, it gives me the usage details but there's no "z" option. If I try the above command without the "z", it works without compression - but the file gets truncated because I don't have enough space in my home directory to store the file uncompressed (the space available is set by a restriction that I have no control over, not by not having enough available disk space). If I run it with the "z", I just get the "tar usage" output to the console, indicating it doesn't understand it.

    I've also tried redirecting the output to gzip:

    tar cvf - /path/dirtocompress | gzip > myarchive.tar.gz
    

    This compresses the directory into one archive file that fits inside my home folder, but doesn't preserve the path structure, so I end up with all the files and directories in the root of the archive.

    I don't have the option to install anything on this Unix server, such as a different version of tar.

    If this was Windows I could just zip up the directory and it would create one archive, containing all files and folders recursively and preserving the path structure. How do I achieve this on Unix using the above constraints?

    • nohillside
      nohillside over 8 years
      tar cvf should create a tar file with directories. If you run zcat archive.tar.gz | tar tvf -, does the output contain the paths?
    • user5639117
      user5639117 over 8 years
      I get tar: Unexpected end-of-file if I run your command, because it's truncated when creating without compression (because of the home folder space restriction).
    • nohillside
      nohillside over 8 years
      Err, the command I propose is just listing the content of your compressed tar file (like ls -l). If you get this error, the tar file itself may be corrupt.
    • user5639117
      user5639117 over 8 years
      I'm on AIX so had to use gunzip -c myarchive.tar.gz | tar -tvf - to get the list to work, but yes, here it displays the paths. So, I guess the process is correct, but WinZip is not doing the decompression properly. Thanks for the help diagnosing this.
  • Zsigmond Lőrinczy
    Zsigmond Lőrinczy over 8 years
    it might be better to avoid absolute paths: cd /sourcepath; tar -cvf - path/dirtocompress | gzip > /outputpath/myarchive.tar.gz