mknod /tmp/oracle.pipe p

sqlplus / as sysdba << _EOF
set escape on

host nohup gzip -c < /tmp/oracle.pipe > /tmp/out1.gz \&
spool /tmp/oracle.pipe
select * from employee;
spool off


rm /tmp/oracle.pip

I need to insert a trailer at the end of the zipped file out1.gz , I can count the lines using

count=zcat out1.gz |wc -l

How do i insert the trailer

T5 (assuming count=5)

At the end of out1.gz without unzipping it.

  • Do I understand you correctly: you want to add a line of text to a zipped file?
    – FelixJN
    Commented Feb 22, 2016 at 9:27
  • Yes without unzipping the file Commented Feb 22, 2016 at 9:30
  • 1
    i) count=zcat out1.gz |wc -l won't work, you need something like count=$(zcat out1.gz |wc -l); ii) zcat is unzipping the file, if you're willing to do that, why don't you wan to unzip? iii) Why don't you count the lines and add the number before compressing the file in the first place?
    – terdon
    Commented Feb 22, 2016 at 9:34
  • 1
    BTW, "to insert at the end" is called "to append", which is exactly what the accepted answer does. Maybe you should edit the title. Commented Feb 22, 2016 at 15:22

3 Answers 3


From man gzip you can read that gzipped files can simply be concatenated:

ADVANCED USAGE Multiple compressed files can be concatenated. In this case, gunzip will extract all members at once. For example:

        gzip -c file1  > foo.gz
        gzip -c file2 >> foo.gz


        gunzip -c foo

  is equivalent to
         cat file1 file2

This could also be done using cat for the gzipped files, e.g.:

seq 1 4 > A && gzip A
echo 5 > B && gzip B
#now 1 to 4 is in A.gz and 5 in B.gz, we want 1 to 5 in C.gz:
cat A.gz B.gz > C.gz && zcat C.gz
#or for appending B.gz to A.gz:
cat B.gz >> A.gz

For doing it without external file for you line to be appended, do as follows:

echo "this is the new line" | gzip - >> original_file.gz
  • This may work for your simple example, as there's hardly anything to zip. Now try with more complex lines which contain overlapping substrings.
    – Murphy
    Commented Feb 22, 2016 at 9:40
  • Seemed to work with adding "Hello" toa cvs file of mine that was zipped from a size of 400kB to 100kB. But I do not know how the compression in gzip works and if if could corrupt anything. So far a few text files I tried were all fine.
    – FelixJN
    Commented Feb 22, 2016 at 9:54
  • Seems like this will indeed work; the blind spot was on my side: stackoverflow.com/questions/8005114/…
    – Murphy
    Commented Feb 22, 2016 at 10:01
  • @Murphy thanks for the clarification - I used this before and you got me worried about my previous data
    – FelixJN
    Commented Feb 22, 2016 at 10:02
  • 1
    what is the | cat - in your last line good for? Should it not work with | gzip >> original_file.gz as well?
    – jan-glx
    Commented Mar 11, 2019 at 15:58

I was wrestling with a similar challenge: appending a few lines to a compressed sql dump. My solution was based on answer from @Fiximan

echo 'append this string' | gzip >> out.gz

How big is your employee table? Unless you have a million employees, and each record takes thousands of bytes, it's probably not even worth the bother of compressing the output.

Almost certainly not worth the bother of compressing it as the output file is being created - so why not just output to uncompressed plain text, run count=$(wc -l out1) ; echo "T$count" >> out1, and then compress it with gzip out1?

alternatively, just run some variation of select count(*) from employee before the spool off command.

  • 1
    The table has 2 million records and uncompressed extract is about 2GB but have a space restriction hence need to compress it on the fly Commented Feb 22, 2016 at 9:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .