LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-04-2008, 05:38 PM   #1
Kcghost
LQ Newbie
 
Registered: May 2006
Posts: 13

Rep: Reputation: 0
Piping ls


Most of us here know what to do if a program like ls gives a huge amount of output that takes up more than the whole screen, you pipe it to "more", or the new and improved "less" or "pg". But its not that new and improved, in fact they all suck for the same reasons.

ls will give you an output that uses all of the screen in several columns, and if its configured like mine will even give useful color coding. ls | more or ls | less strips all color and lists output into individual lines.

Is there currently available any better output program than "less", is there any way to display ls the way it should be, untampered with just pausing at every screenfull? And hell, if there isn't an easy way to do this, shouldn't there be? Its a pretty common problem thats encountered very frequently, and its solved only halfass with less or more. Somebody should make a much better "more". Give me more years of experience and programming and some source code...or somebody else do it, lol.
 
Old 03-04-2008, 05:51 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The color info & multicolumn formatting isn't present when you pipe the output to the less or more program. Look at the output of "ls | cat". Cat simply takes the stream on stdin and outputs it to stdout. It doesn't strip anything. If the color & multicolumn information were present when piping the output of ls, then something like "for file in *.txt; do ..." wouldn't work.
 
Old 03-04-2008, 05:59 PM   #3
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
If the output of a command does not span too many pages you could always view it by Shift+PgUp/PgDwn
 
Old 03-04-2008, 06:36 PM   #4
Kcghost
LQ Newbie
 
Registered: May 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks, I didn't know about the Shift>PgUp/PgDown, that's very useful.
 
Old 03-04-2008, 06:47 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
How about this:
Code:
ls -C -w $COLUMNS --color=always | less -R
 
Old 03-05-2008, 12:41 PM   #6
Kcghost
LQ Newbie
 
Registered: May 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Nice, "ls -C -w $COLUMNS --color=always | less -R" was exactly what I was looking for, I have no idea how that works or how you figured that out, but thanks. I just added it as an alias and im good to go.
 
Old 03-05-2008, 01:38 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Just checking up on this: you did grasp that the "fault" for the behaviour
you describe in post #1 of this thread is with ls, not with the pagers?
ls behaves differently (by default) when writing to a pipe.


Cheers,
Tink
 
Old 03-05-2008, 04:38 PM   #8
Kcghost
LQ Newbie
 
Registered: May 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Yes I did grasp that, although it seems strange to me that ls should act so differently when piped that you have to issue it special commands to keep its formatting the same. I can only assume ls was coded this way so that you could pipe ls to more or less without the -R option for the colors, because without the -R (raw colors option) less displays strange characters and screws up the formatting; and more will pass the colors but screw up the formatting; cat however, passes it on fine by default.
 
Old 03-05-2008, 04:59 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Well... the default behaviour is actually because you're supposed to be
able to pipe into ANYTHING, not just pagers ... for example if you want
to count the number of files in the directory and you use wc -l, it makes
no sense to have columnised output (Of course you "could" count -w, but
then files with spaces in their name would screw it up).

If you want to count files with two different sub-strings in their names
using awk, columns make no sense ...

I'm sure you could think of other examples, too :}


Cheers,
Tink
 
Old 03-05-2008, 07:24 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Kcghost View Post
I just added it as an alias and im good to go.
I hope you named the alias something different from “ls” (since many scripts will assume it produces non-columnated, non-colored output).
Quote:
Originally Posted by Kcghost View Post
although it seems strange to me that ls should act so differently when piped that you have to issue it special commands to keep its formatting the same.
Think of it this way: the default behavior of ls is to write non-columntated, non-colored output to stdout (regardless of whether stdout is a file or a pipe or a socket or whatever). The columns and colors are extra bells and whistles which go into effect when stdout is detected to be connected to a terminal (with isatty(0)). Columns and ANSI colors don’t usually make sense without a terminal to interpret them.

For example, if you want to save the directory listing to a file, how would the command
Code:
ls > saved_listing
know how wide to make the output if there are columns? What about colors (they might show up as funky symbols if you view the file in a text editor which doesn’t understand ANSI escape sequences)?

Of course there are other times where you want to tell the utility explicitly to use columns of a certain width and colored output, even if stdout is not a tty (which was your original question).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Piping... disruptive Programming 3 02-12-2008 01:09 PM
Help with piping mijohnst Linux - General 7 10-21-2005 04:14 PM
piping /forking geminigal Programming 3 04-10-2005 09:49 PM
Piping question OtisLinux Linux - Software 1 02-12-2004 01:38 PM
piping data tearinox Linux - Newbie 9 12-09-2003 01:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration