Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > "ls" command question?

"ls" command question?
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2003
Status: Offline
Reply With Quote
Sep 16, 2003, 10:20 PM
 
Ok, here's a real stupid question:

How do I tell the "ls" command to output my data in 2 columns?

This is what I've already learned that will not work:

ls --width=45 (this works but is not reliable since the output size can change)
ls -1 (this outputs in single column format, ls -2 doesn't work (why!))
ls -x (this is ls across and I don't understand what it's useful for)

Someone please help me, I've spent over an hour and a half trying to get this to work! I know this is silly, but I need this to work and am willing to hear of suggestions.
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Sep 16, 2003, 11:47 PM
 
Shrink your terminal window?

No really...you might have to use a shell/Perl/whatever script.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Dedicated MacNNer
Join Date: Sep 2003
Location: Pittsburgh, Pennsylvania
Status: Offline
Reply With Quote
Sep 16, 2003, 11:56 PM
 
Originally posted by pwharff:
Ok, here's a real stupid question:

How do I tell the "ls" command to output my data in 2 columns?

This is what I've already learned that will not work:

ls --width=45 (this works but is not reliable since the output size can change)
ls -1 (this outputs in single column format, ls -2 doesn't work (why!))
ls -x (this is ls across and I don't understand what it's useful for)

Someone please help me, I've spent over an hour and a half trying to get this to work! I know this is silly, but I need this to work and am willing to hear of suggestions.

If you want columns

ls -laF gives you a few columns with different items, you could always pipe it through grep and sed and spit out exactally what you want.

Nate
     
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Sep 17, 2003, 12:25 AM
 
Umm, maybe you *can't*. That is, it's probably not a built in. The available options look to be a single column (default when used into a pipe) or the multicolumn output that's used where the result is being displayed on a screen, or the "-x" option, which sorts across the page instead of down. Displaying in two columns isn't exactly a canonical sort of operation.

You can just pipe the output of ls into a script if you want to get the 2 column approach: here's the result of 2 minutes scripting. Ugly as hell, but does the job. Adjust as necessary, and pretty it up somebody: please.

Cheers,
Paul

% ls | perl -ne 'chomp($one=<>);chomp($two=<>);printf "%-30s %-30s\n",$one,$two'
     
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
Sep 17, 2003, 09:46 PM
 
here's the result of 2 minutes scripting. Ugly as hell, but does the job. Adjust as necessary, and pretty it up somebody: please.

ls | perl -ne 'chomp($one=<>);chomp($two=<>);printf "%-30s %-30s\n",$one,$two'
Hmm... seems to drop the first file, and every third file thereafter. How about:

ls | perl -pe 's/(.*)\n/(pack "A30",$1).<>/e'
     
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Sep 17, 2003, 11:26 PM
 
Gack: thanks for the kick in the arse. What I vomited forth should have looked like

ls | perl -ne 'chomp;chomp($b=<>);printf "%-30s %-30s\n", $_,$b'

which has the virtue of actually printing all the files. But your pack version is definitely a bit cuter.

Cheers,
Paul
(Last edited by Paul McCann; Sep 18, 2003 at 02:39 AM. )
     
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 18, 2003, 03:57 AM
 
Originally posted by Paul McCann:
Gack: thanks for the kick in the arse. What I vomited forth should have looked like

ls | perl -ne 'chomp;chomp($b=<>);printf "%-30s %-30s\n", $_,$b'

which has the virtue of actually printing all the files. But your pack version is definitely a bit cuter.

Cheers,
Paul
You guys are making this all too complicated. All you need:

ls | rs 0 2

That's it!!!
`ls' output in 2 columns!!!

     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Sep 18, 2003, 10:59 AM
 
Originally posted by VEGAN:
You guys are making this all too complicated. All you need:

ls | rs 0 2

That's it!!!
`ls' output in 2 columns!!!

The only problem is, if you have a space in a file name, this will not work.

Code:
[arkham@flybook ~/junk] touch "a b c d" [arkham@flybook ~/junk] touch "d e f g" [arkham@flybook ~/junk] ls | rs 0 2 a b c d d e f g
So that's not quite it.

How about this:

Code:
[arkham@flybook ~/junk] ls -l total 0 -rw-r--r-- 1 arkham staff 0 Sep 18 11:58 a b c d -rw-r--r-- 1 arkham staff 0 Sep 18 11:58 d e f g -rw-r--r-- 1 arkham staff 0 Sep 18 12:01 g h i j -rw-r--r-- 1 arkham staff 0 Sep 18 12:01 j k l m [arkham@flybook ~/junk] ls -1 -Q | sed 's/ /%%/g' | rs 0 2 | sed 's/%%/ /g' "a b c d" "d e f g" "g h i j" "j k l m"
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Sep 18, 2003, 08:17 PM
 
rs is a cool little utility that I knew nothing about: thanks for the pointer. While a bare "rs 0 2" won't do the trick if you use

ls | rs -c 0 2

the output looks OK to me. That causes the "columns" to be separated with a "^I" (whatever that is!) instead of space. I guess if your file name contains ^I you'd still be in trouble, but that doesn't sound terribly likely. If you know a character that's definitely not used in your filenames then inserting it directly after the -c will also work.

Cheers,
Paul
     
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 19, 2003, 08:01 AM
 
'^I' or 'Control-I' is the TAB character. Although it's unlikely to have one in a file name, you can always be sure not to have a '/' in it. But then you can write as well ls -q | rs -e 0 2 (the -q to replace a newline (and other funny characters) with a '?').

However, the output looks bad if a filename is too long and then causes all lines to wrap. And that's the reason why ls has no -2 option: it either fits the output dynamically in as many columns (of different width) as possible to get you a short listing, or it serves you each entry on a single line if you find that easier to view or like to process it any further.

-
     
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
Sep 19, 2003, 03:45 PM
 
Well if you just want a tab between filenames, then this will work:

ls | perl -pe 's/\n/"\t".<>/e'

Unlike rs, this works properly when filenames contain spaces.
     
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 19, 2003, 06:05 PM
 
Originally posted by Rainy Day:
Well if you just want a tab between filenames, then this will work:

ls | perl -pe 's/\n/"\t".<>/e'

Unlike rs, this works properly when filenames contain spaces.
The above solutions do work properly when a filename contains spaces and they do not eat the final linefeed like yours does.

If someone really wanted tabs as column separator, rs would do that too with the whole command being shorter and using less resources.

-
     
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
Sep 19, 2003, 06:37 PM
 
The above solutions do work properly when a filename contains spaces
And indeed it does appear to work! I stand corrected. I was thrown by man rs which states:
Code:
RS(1) RS(1) NAME rs - reshape a data array SYNOPSIS rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ] DESCRIPTION Rs reads the standard input, interpreting each line as a row of blank-separated entries in an array, and writes it on the stan- dard output...
Looked to me, from that, that spaces would be treated as delimiters.
     
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Sep 20, 2003, 02:25 AM
 
I had to read the man page twice but it was worth it ... at the end they admit it has too many options

-
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:30 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2