1

I have one doubt with this:

for i in ./*.gz
do
gunzip -c $i | head -8000 | gzip > $(basename $i)
done

I get an "unexpected end of file" error for gzip, and I don't get the 8000 at all, only a few tens of lines. I've checked the integrity of my files and they are fine. Curiously, if I run for individual files:

gunzip -c file1.gz | head -8000 | gzip > file1.gz

I get the expected result. With this script I can obtain the first 8000 lines from a compressed file with 10708712 lines, and compress again. The new file overwrites the original file, but that's ok.

John WH Smith
  • 15,880
zorbax
  • 340
  • 3
    If the individual invocation worked for you you just got lucky. Redirecting back into a file that you're reading is usually not a good idea. – tink Oct 29 '14 at 19:45

1 Answers1

4

You should write the output to a temporary file and then rename:

for i in ./*.gz 
do 
  gunzip -c "$i" | head -8000 | gzip > "$i.tmp"
  mv -f "$i.tmp" $(basename "$i") 
done

That your version sometimes works is if you gunzip buffers enough while reading.

Anthon
  • 79,293