6

I have a zipped text file a.zip I want to read the first 10 lines of it. Is it possible to do that without unzipping the whole file?

10

This simple pipe-script works for me:

zcat a.zip | head -n 10

Here:

  • zcat a.zip - unpacks zip-archive and sends its contents to standard output
  • | pipes zcat output to head input
  • head -n 10 - shows first 10 lines from its standard input
  • this unzips the whole file. – yukashima huksay Jan 4 '18 at 11:41
  • 5
    @yukashima I'd guess it depends on the size of a.zip. When head terminates (after printing 10 lines) the zcat (which is gzip -dc behind the scenes) should receive a SIGPIPE and stop unzipping. – PerlDuck Jan 4 '18 at 11:53
  • @yukashimahuksay How do you know it unzips the whole file? I think all zip files keep the file list at the end of the archive, maybe it's just reading the whole file but not actually unzipping it – Xen2050 Jan 4 '18 at 12:38
  • I can confirm by example that it does not unzip the whole file as intended. zcat verybigfile.zip | head -10 prints the first 10 lines immediately. A full unzip would take ca. 10 minutes. – user27164 Aug 21 '20 at 14:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.