less command does not open zip files on debian

Super User Asked by M.J on December 16, 2020

Here I am using debian/testing distribution on my machine but there is something interesting.

# less
"./" may be a binary file.  See it anyway?

Do I need to install some extra packages on my machine in order to be able to open zip files on my machine using less command?

2 Answers

The lesspipe tool can transparently convert files to text when you call less on them. This includes decompressing .gz files and listing the contents of .zip files. Debian includes lesspipe in the less package but doesn't automatically configure it.

Add the following to your shell's login script (e.g, ~/.bash_profile):

eval "$(lesspipe)"

This should add two environment variables, LESSOPEN and LESSCLOSE. Once you've done this, running less on a zip file should show you the list of files in the zip file.

Correct answer by satwell on December 16, 2020

tl;dr: unzip -p | less

Some background


I think the ZIP file format first appeared on MSDOS and then MS Windows. It was primarily a way to gather a group of many files together in an archive and compress the combined data. In other words it creates a multi-file compressed archive. You cannot assume a ZIP file contains a single file and assume that file is a text file.

I recall the compression algorithms used by zip utilities changed over the years, though probably later tools could handle earlier compression algorithms. I think the most common tools for creating ZIP files were originally the PKZIP tools created by Phil Katz at PKWare. Later MS WIndows acquired the ability to create ZIP files natively.

Unix & Linux

On Unix-like systems the traditional equivalent method was to first gather multiple files together using a tool such as tar to create a something.tar file and then separately compress it to create a something.tar.Z file. Later Gnu's tar acquired compression capabilities from Gnu-Zip (gzip) and could create a something.tar.gz or something.tgz in a single operation.

So a file with a filename extension of .ZIP is most likely to have originated on Windows not Linux.

If your ZIP file definitely contains a single text file, a general solution would be to pipe the output of an unzip operation into less. This depends on having an unzip command that reads your particular flavour of zip compression and has options to write to STDOUT.

Commonly installed tools for viewing text files compressed using some "zip" compression algorithm include zcat and bzless. The man pages for these say

GZIP(1)                                                  General Commands Manual    
       gzip, gunzip, zcat - compress or expand files

       gzip [ -acdfhklLnNrtvV19 ] [--rsyncable] [-S suffix] [ name ...  ]
       gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ...  ]
       zcat [ -fhLV ] [ name ...  ]

       Gzip  reduces  the  size of the named files using Lempel-Ziv coding (LZ77).


BZMORE(1)                                                General Commands Manual 

       bzmore, bzless - file perusal filter for crt viewing of bzip2 compressed text

       bzmore [ name ...  ]
       bzless [ name ...  ]

But this depends on the chosen compression utilities knowing the particular compression algorithm used on your zip file.

PKZIP compatible Unzip

Most likely you need an unzip tool that is compatible with the DOS/Windows ZIP format. On some Linux systems you may need to install this first using whatever package manager is appropriate for your distribution. For example sudo apt install zip.

$ ls -l eg*
-rw-rw-rw- 1 rgb rgb 182 Aug 30 19:10

$ unzip -l
  Length      Date    Time    Name
---------  ---------- -----   ----
       20  2020-08-30 18:50   eg.txt
---------                     -------
       20                     1 file

$ unzip -p
hello world
The end

$ unzip -p | less
hello world
The end

Answered by RedGrittyBrick on December 16, 2020

Add your own answers!

Related Questions

Mouse on windows only works properly with USB hub

0  Asked on November 16, 2021 by chubby-unicorn


PC startup issue: Can a video card short damage a motherboard?

1  Asked on November 16, 2021 by jeffrey-g-jarjoura


Make internal card reader readable to BIOS

1  Asked on November 14, 2021 by starkers


Turn on mobile hotspot on startup Windows 10

2  Asked on November 14, 2021 by george-dimitriadis


Router technologies to bypass NAT?

1  Asked on November 14, 2021


How to troubleshoot miracast on windows 10 laptop?

1  Asked on November 14, 2021 by jonesome-reinstate-monica


How to set +60Hz refresh rate with an Intel CPU?

3  Asked on November 14, 2021 by user1066981


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir