On Fedora/Redhat/CentOS the less command seems to magically detect a gzipped file and decompress it on the fly, so you can do:

less my_stuff.csv.gz

I've just noticed this doesn't work on Ubuntu 11

less my_stuff.csv.gz
"my_stuff.csv.gz" may be a binary file.  See it anyway? 

I've been examining my CentOS VMs to see if there's some shell alias magic that makes it work but there doesn't seem to be. Is gzip support just built in to the CentOS binary?

If anyone knows how this works on CentOS and/or how it can be made to work on Ubuntu I'd be grateful.

I'm aware I can do

zcat my_stuff.csv.gz | less

but that would make my keyboard wear out more quickly.

  • 2
    On Ubuntu 16.04 less myfile.txt.gz works out-of-the-box. – Pablo Bianchi Sep 29 '17 at 1:21

less do not do that by default, but there is a line in the default ~/.bashrc that change the default behavior:

[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

Be sure to not remove or comment this line.
With this in place, you can page zipped and non-zipped files without knowing, like in

less /var/log/dpkg.log*

where some of the logs can be zipped and some others non-zipped.

For the root account accessed through sudo you cannot do the same thing, but there are two workarounds:

  1. sudo zless file*
  2. sudo -E less file*

In the first case I used zless, that works also for non-zipped file.
In the second one I added the -E option to sudo to preserve your environment variables, to make less work as you would expect.

  • 1
    Isn't the first line present in .bashrc by default? I can't remember adding it and it works seamlessly here (10.4). – johanvdw Sep 23 '11 at 7:34
  • 2
    @johanvdw: yes it is: I said "there is a line in the default ~/.bashrc". The OP could have modified its own ~/.bashrc. – enzotib Sep 23 '11 at 7:35
  • Just thought I'd add, that on the rhel OS's mentioned where this works, you will see the following in env: LESSOPEN=|/usr/bin/lesspipe.sh %s This is done by a script containing [ -x /usr/bin/lesspipe.sh ] && export LESSOPEN="|/usr/bin/lesspipe.sh %s" added to /etc/profile.d/less.sh – KHobbits Mar 31 '17 at 13:33

Less can't read gzipped files, you can check it in its man page. There is a script, installed by default in Ubuntu and most distros, called zless, that works as you described.

So the answer is to use:

zless my_stuff.csv.gz

The difference between Fedora and Ubuntu is that Fedora set the environment variable LESSOPEN to force the argument through lesspipe:

||/usr/bin/lesspipe.sh %s

Note that this command is not going to work in Ubuntu.

  • On Ubuntu zless is a shell script that uses less and lesspipe in its core, so less cannot be a symlink to zless. I do not think it is different in other distros. – enzotib Sep 23 '11 at 7:42
  • Oh... you made me boot my Fedora install ;). Edited. – Javier Rivera Sep 23 '11 at 8:13

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.