1

Constantly switching between Linux and FreeBSD I keep bumping into this. When I do:

 less something.tgz

Linux systems(well... Ubuntu to be precise) show me a list of files, which is what I generally want, while FreeBSD shows a binary contents. How is this behavior(showing file contents) is accomplished on linux and is it possible to replicate it on FreeBSD systems?

5 Answers 5

4

Have a look at the man page of lesspipe (or lessopen) on an Ubuntu box.

With the Debian/Ubuntu version, all you have to do is to add

eval "$(lesspipe)"

to your .bashrc.

On FreeBSD you find the lesspipe.sh script in a separate port named lesspipe, which uses a version from sourceforge (that doesn't have the convenient setup above, though -- the man page of less has more info).

Here is a different version of the script I found on freshmeat: lesspipe, an addon for the browser less

2
  • 1
    Were I you I'd be very very careful with the eval command.
    – chris
    Nov 4, 2009 at 15:11
  • yawn lesspipe sets two environment variables. to be careful, use absolut paths. don't use this as root.
    – user1686
    Nov 5, 2009 at 1:28
8

I'm going to be the cranky old man here who yells "hey you kids, get off my lawn!" but...

You're not doing it right.

Less, and more, the two common pager programs, are meant to be run at the end of a string of piped commands. These programs aren't designed to read any random sort of file like a cpio archive or a tar archive or a uuencoded gzipped tarred des encrypted bzip2ed file.

Learn the unix way of doing things and you'll start to understand what's going on with a whole bunch of other things as well. This way you'll learn about things like stdin and stdout and stderr.

gzip -d < file | tar -tvf - | less

You'll thank me later.

3
  • 2
    Refusing to use tar -z is just a little too much, IMO. Oct 20, 2009 at 19:04
  • Habit from the old country. At this point it is just built into my fingers and it has the virtue of never surprising me no matter how broken or old the system I'm typing it on happens to be.
    – chris
    Oct 20, 2009 at 23:54
  • chris, I appreciate your advice :) I am far from having one-liners ingrained in my brain, but certainly heading this way ;)
    – Alex N
    Oct 23, 2009 at 22:04
6

You might see something useful with zless somefile.tgz but I think what you really want to do is this:

tar ztvf somefile.tgz |less

which will work on all systems. Note that the t just tells you what is there unlike x which extracts files.

2
  • 1
    the advantage of the lesspipe script on debian is, that it knows about several different archiver formats, as well as some image formats, so you don't have to remember any additional commands (or even the fact that you have to pipe something into less)
    – user1686
    Oct 20, 2009 at 16:09
  • "which will work on all systems" actually not true. The default tar on AIX does not understand GZIP'd files... and you're forced to use a command like @Chris provided: "gzip -d < file | tar -tvf - | less"
    – oz10
    Dec 25, 2009 at 1:44
2

This is done using the 'lessopen' preprocessor:

michael@challenger:~> echo $LESSOPEN
lessopen.sh %s
michael@challenger:~> which lessopen.sh
/usr/bin/lessopen.sh
michael@challenger:~> less logs.zip
Archive:  ./logs.zip

 Length   Method    Size  Ratio   Date   Time   CRC-32    Name

--------  ------  ------- -----   ----   ----   ------    ----
   17484  Defl:N     2269  87%  03-30-09 22:59  03bf67d6  20090330/Logs-20090330.CSV
...

Remove the environment variable and you see less behaves like it does on a basic system:

michael@challenger:~> unset LESSOPEN
michael@challenger:~> less logs.zip
"logs.zip" may be a binary file.  See it anyway? 

Check your less(1) man page to see if it supports LESSOPEN.

1

I've never seen this work on .tgz files, only .gz text files, but maybe it does. In any case, on many Linux distros less will detect if a file is gzipped and unzip the stream as it displays it. I can't speak for FreeBSD, but at least on OpenBSD you need to use zless instead if looking at a gzipped text file. I imagine it's included on FreeBSD as well, or at least available from the ports tree.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .