Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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 :}
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.