gzip 2 files into one file
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:
-
c
reate a new archive - g
z
ip (alternatives:j
for bzip2,J
for xz) -
v
erbose (list processed files; optional) - output
f
ile (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.
Related videos on Youtube
Mohammad Kholghi
Updated on September 18, 2022Comments
-
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 over 5 yearsWhat a great answer
-
vaquito over 5 yearsThe 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 over 5 yearsLittle known fact: It is possible to compress multiple files with
gzip
. But it's not very useful since when decompressing such a file withgunzip
it will just append all the original files into a single file. If you typeecho Foo > a ; gzip a
you will have a file calleda.gz
which contains both the name and timestamp of the originala
file. If you then typeecho Bar > b ; gzip b ; cat a.gz b.gz >combined.gz
you will have acombined.gz
file which contains all the information needed to reconstructa
andb
with their original names, contents, and timestamps.