Add one line at the top of the huge gzip file

I have a huge gzip file (~ 400MB). I want to add one line of text to the BEGINNING of the file.

I was thinking to create a gzip file with a header line and then using zcat

concatenate the header file and the log file. Just wanted to check if there is a better / elegant / efficient way to do this.

+3


source to share


2 answers


two gzipped files merged into one file is actually a valid gz file.

Try it.

Gzip your first, only line you want to add, then put two by third.



print "My newline" | gzip -c > /tmp/smallzip.gz
cat /tmp/smallzip.gz mybigfile.gz > newbigfile.gz 

      

This will save time and cpu to decompress a large gz file by adding your line and rezipping, which would be:

(
    echo "My newline"
    zcat bigfile.gz
) | gzip -c > newbifile.gz 

      

+7


source


This should work:

gzip < newlineoftext > newfile.gz
cat oldfile.gz >> newfile.gz

      



(Because, as another answer already pointed out, the two shared gzipped files are a valid gzip file.)

+3


source







All Articles