How to Delete a Tar File After I Gzip It?

I am creating a tar file and then I am gzipping the tar file. So I am getting a .tgz file and now I want to delete the previous tar file.

${gzipExe} -f --rsyncable $tarname.TAR --stdout >> $tarname.TGZ
12

2 Answers

I usually zip the file at the same time by using the 'z' parameter with the tar command:

tar czvf allmyfiles.tar.gz *

The above command creates a new archive file called allmyfiles.tar.gz that contains all the files in the current folder, and zips it.

Otherwise, just delete the .tar file with the next command:

$rm $tarname.TAR

Note that .tar.gz is more commonly used than .tgz.

You have a couple of options. Assuming you need that --rsyncable option:

  • Compress and tar in one step (without an intermediate tar file):
REM I think you are on Windows so...
set GZIP=--rsyncable
tar czf file.tar.gz files
  • Pipe the output of tar into gzip (without an intermediate tar file)
tar c files | gzip --rsyncable > file.tar.gz
  • Archive and compress in two steps.
tar -cf file.tar files
gzip -f --rsyncable file.tar

If you simply run gzip file.tar instead of gzip --stdout file.tar > file.tar.gz gzip will delete the tar file for you.

Also note that using gzip --stdout file.tar >> file.tar.gz as in your question will actually append the new gziped contents to file.tar.gz which is probably not what you want.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest