How to turn a tar file to a tgz file

gziptar

I usually create a tgz file for my_files with the command tar -czvf my_files.tgz my_files, and extract them with tar -zxvf my_files.tgz. Now I have a tar file created with the command tar -cvf my_files.tar my_files. I'm wondering how I can turn the my_files.tar into my_files.tgz so that later I can extract it with the command tar -zxvf my_files.tgz? Thanks.

Best Answer

A plain .tar archive created with cf (with or without v) is uncompressed; to get a .tar.gz or .tgz archive, compress it:

gzip < my_files.tar > my_files.tgz

You might want to add -9 for better compression:

gzip -9 < my_files.tar > my_files.tgz

Both variants will leave both archives around; you can use

gzip -9 my_files.tar

instead, which will produce my_files.tar.gz and delete my_files.tar (if everything goes well). You can then rename my_files.tar.gz to my_files.tgz if you wish.

With many tar implementations you can extract archives without specifying the z option, and tar will figure out what to do — so you can use the same command with compressed and uncompressed archives.

Related Question