Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 4 down vote accepted

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

share|improve this answer
1  
Were I you I'd be very very careful with the eval command. –  chris Nov 4 '09 at 15:11
    
yawn lesspipe sets two environment variables. to be careful, use absolut paths. don't use this as root. –  hop Nov 5 '09 at 1:28

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.

share|improve this answer
2  
Refusing to use tar -z is just a little too much, IMO. –  grawity Oct 20 '09 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 '09 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 '09 at 22:04

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.

share|improve this answer
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) –  hop Oct 20 '09 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" –  paxos1977 Dec 25 '09 at 1:44

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.