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?
Asked
Active
Viewed 9.7k times
81

Gilles 'SO- stop being evil'
- 829,060

Ryan C. Thompson
- 5,438
-
11+1 for introducing me to zless – R.D. Alkire Mar 19 '21 at 02:20
3 Answers
81
Use zcat
, then pipe it to less
.
$ zcat $FILE | less

Michael Mrozek
- 93,103
- 40
- 240
- 233

Alex
- 819
-
3
-
4Unlike 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
-
1A 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

Gilles 'SO- stop being evil'
- 829,060
-
1This is the answer that actually solves the question: how to not have to type the
z
inzless
- automatically uncompressing rather than having to remember to add thez
– Cornelius Roemer Aug 28 '22 at 20:09 -
On Redhat based distributions,
less
can open compressed files, and i see no.lesskey
or no alias or no specials parameters to the command – Alex F Nov 13 '23 at 10:50 -
@AlexF It's done with environment variables.
/etc/lesskey
would be another possibility. – Gilles 'SO- stop being evil' Nov 13 '23 at 11:15
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