Linux pv | gzip doesn't delete original file

3

I'm pretty new to unix so go easy.

Normally gzip file replaces file with file.gz (which i'm guessing is: create file.gz then rm file).

I have some rather large files I'm compressing and I like to see the progress and eta so I use

pv -tpreb file | gzip -9 > file.gz

But now I am left with the original file along with the new file.gz. I don't want to have to rm the original manually.

What should I do?

danielr

Posted 2016-01-12T02:01:07.677

Reputation: 45

4It seems that you cannot. pv reads the files and pipes to gzip therefore gzip gets its input from stdin. Of course gzip will have no clue that the input actually comes from file and therefore cannot delete it. – Kenneth L – 2016-01-12T02:22:29.323

Answers

3

As Kenneth L mentioned in his comment, there's no way to accomplish this with the tools you're using, due to the way pv pipes file to gzip.

A kludgy workaround would be to delete the original file immediately after compressing it:

pv -tpreb file | gzip -9 > file.gz && rm file

You can compress multiple files in the same archive and delete the originals with the following commands:

export FILES='foo bar quux'
tar c $FILES | pv -tpreb | gzip -9 > zip.gz && rm $FILES

and decompress those files with tar xvzf zip.gz.

nc4pk

Posted 2016-01-12T02:01:07.677

Reputation: 8 261

I did not know you could append commands to eachother with &&. Thanks! I'd +1 but don't have the neccessary requirements from SE. – danielr – 2016-01-12T04:38:59.417

1As an addition, I edited my bash profile with the function gz() { pv -tpreb $1 | gzip -9 > $1.gz && rm $1 } allowing me to call the shortcut gz file. I hope that's useful to someone. An accompanying gunzip shortcut is a good idea too. – danielr – 2016-01-12T05:24:50.443