26

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.

1 Answer 1

42

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.

2
  • How about the reverse? tar.gz into tar? Commented Oct 11, 2021 at 6:45
  • 1
    @benathon gunzip < my_files.tar.gz > my_files.tar, or gunzip my_files.tar.gz if you don’t want to keep the compressed archive. Commented Oct 11, 2021 at 6:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.