gzip 2 files into one file

14,329

Solution 1

Unlike ZIP, RAR or 7-Zip, for example, gzip can only compress one file. As you’ve already noted, there is the tar program which can serialize multiple files into one which makes them ready for gzip. The traditional Unix philosophy prefers to use multiple simpler and more specialized tools than one monolithic and complicated. In this case, it results in using tar and gzip consecutively, resulting in a .tar.gz (or .tgz) file.

However, GNU tar includes the -z option which compresses the result of the traditional tar using gzip in just one command.

.tar.gz files are mostly created using the -czvf options:

  • create a new archive
  • gzip (alternatives: j for bzip2, J for xz)
  • verbose (list processed files; optional)
  • output file (the following argument specifies the output file)

In your example, you can use the command:

tar -czvf test.tar.gz file.mp4 bar.txt

Is there any way to watch inner files without decompression with gzip or bzip2?

Yes, your command also works for a .tar.gz file:

tar -tvf test.tar.gz

Further reading

Solution 2

gzip is a compressor, not an archiver, but it works well with tar

tar -cvzf file.tar.gz path-to-files-or-directories-to-compress

See man tar

Compression options
   -a, --auto-compress
          Use archive suffix to determine the compression program.

   -I, --use-compress-program=COMMAND
          Filter data through COMMAND.  It must accept the -d option,  for
          decompression.  The argument can contain command line options.

   -j, --bzip2
          Filter the archive through bzip2(1).

   -J, --xz
          Filter the archive through xz(1).

   --lzip Filter the archive through lzip(1).

   --lzma Filter the archive through lzma(1).

   --lzop Filter the archive through lzop(1).

   --no-auto-compress
          Do not use archive suffix to determine the compression program.

   -z, --gzip, --gunzip, --ungzip
          Filter the archive through gzip(1).

   -Z, --compress, --uncompress
          Filter the archive through compress(1).

And yes, you can watch compressed archives the same way.

Share:
14,329

Related videos on Youtube

Mohammad Kholghi
Author by

Mohammad Kholghi

Updated on September 18, 2022

Comments

  • Mohammad Kholghi
    Mohammad Kholghi over 1 year

    I want to gzip two or more files into one file, I checked this and this but both have files like: n1.txt, n2.txt, ... but my filenames are completely different, like: file.mp4, bar.txt, foo.jpeg and I want to gzip them all and put the output into one file. This didn't help too:

    gzip -c file.mp4 > test.gz
    gzip -c bar.txt >> test.gz
    

    Do I have to tar them first?

    And another question: In tar files, we can watch inner files without decompression using:

    tar -tvf filename.tar
    

    Is there anyway to do this with gzip or bzip2?

  • mike3996
    mike3996 over 5 years
    What a great answer
  • vaquito
    vaquito over 5 years
    The combination of two tools has also allowed the replacement of the compression method with others more efficient for a specific need. Modern versions of tar knows many compressors and can transparently invoke them when needed.
  • kasperd
    kasperd over 5 years
    Little known fact: It is possible to compress multiple files with gzip. But it's not very useful since when decompressing such a file with gunzip it will just append all the original files into a single file. If you type echo Foo > a ; gzip a you will have a file called a.gz which contains both the name and timestamp of the original a file. If you then type echo Bar > b ; gzip b ; cat a.gz b.gz >combined.gz you will have a combined.gz file which contains all the information needed to reconstruct a and b with their original names, contents, and timestamps.