81

I am using Ubuntu, and I would like to be able to type less compressed_text_file.gz and page the contents of the text file in uncompressed form. Is there a way to do this?

3 Answers3

81

Use zcat, then pipe it to less.

$ zcat $FILE | less
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
Alex
  • 819
  • 3
    I would prefer if less just worked on compressed files. – Ryan C. Thompson May 06 '11 at 18:37
  • 4
    Unlike the accepted answer this one is a clean and unix-way solution. Thanks! – Pavel Vlasov Dec 08 '14 at 11:43
  • This is as simple as it gets, win. One thing to bear in mind is that if you open a large file with less, it opens in a useable way, straight away. With this approach, it will open but you won't be able to do much (if anything) until the compression has finished running it all through the pipe. So if you're planning on opening it repeatedly, it's better to decompress it first. Otherwise, do this and check your emails or something :) – Max Williams Jul 04 '18 at 07:31
  • 1
    A silly question gets a silly top answer: why not just use zless? – Cornelius Roemer Aug 28 '22 at 20:08
51

You can configure the key bindings and set many settings for less in a file called ~/.lesskey. Once you've created the file, run the lesskey command; it generates a file called ~/.less which less reads when it starts.

The setting you want is LESSOPEN. It's an input formatter for less. The less package comes with a sample formatter in /bin/lesspipe; it decompresses gzipped files, shows content listings for many multi-file archive formats, and converts several formatted texts formats to plain text. In your ~/.lesskey:

#env
LESSOPEN=|/bin/lesspipe %s
9

I'm using IBM and when using zcat, it will complains that it can't find the file ending with .Z.

On IBM one can use gzcat:

$ gzcat log_file.gz | less
Viet
  • 211