ls command - define number of columns

43,704

Solution 1

You will have to pipe | your ls output. Programs like sed or cut can sort out data as long as you know what your delimiter is. Most often in column spacing your delimiter is a tab.

However I don't know of many ways or reasons that you would want to split your ls output into columns. You can always pipe your output to more and it will make your data easier to read.

Solution 2

No. ls does not have this capability (and few would want it to).

Solution 3

Just override the width parameter with 1:

ls -w 1

Works great for me!

Solution 4

Unfortunately, ls doesn't support this. You'd have to do something like ls -1 | xargs -n 2 (or just ls | xargs -n 2 should also work).

Solution 5

I tested these command on Linux Debian in the terminal. If your terminal is wide enough, just stating "ls" will give columns but it won't use all the space (width) of the terminal. I made the terminal as wide as the screen and "ls" just produced 4 columns. Problem is if you pipe this into "more" you loose the columns.

Next I used "ls -w200" in the terminal as wide as the screen (200 is the 'assumed screen width'). I got 5 columns (instead of 4 previously with just "ls") now and that takes up the full width of my screen -- everyone knows that this will be affected by the length of your file names. Now it is one more column then "ls" by itself and the full screen width is used. Again, like "ls", if I pipe the command into "more" ("ls -w200 | more") you loose the columns (i.e. just have one column).

So this is as close as you can get to multiple columns.

Note: I tried the command "ls -w2000" with terminal width maximized. The output was one line with a "wrap around" with 2 spaces between directory entries. It looks messy but displays all entries in the directory in the shortest way.

Share:
43,704

Related videos on Youtube

erch
Author by

erch

my about me is blink at the moment

Updated on September 18, 2022

Comments

  • erch
    erch almost 2 years

    Is there a way to define the number of columns in ls output?

    I got this far: ls -C sorts in columns and ls -1 produces one column. --tabsize = option to produce two columns resulted in what looks like this image here [which originates in this post].

    Learning that the the command assumes that a column is eight characters wide I tried width= and but didn't get far.

    Question: How to define the number of rows with ls 'on-board tools' → without another command, like column?

    • Hauke Laging
      Hauke Laging about 11 years
      ls -c sorts by ctime.
    • erch
      erch about 11 years
      @HaukeLaging edited the question; thanks for the hint!
    • coder
      coder about 11 years
      You can also just write your own script, I did so some years ago and use it all the time: synodins.com/scripts/ls_crystal. By either writing your own or using mine you have complete control over how the script operates.
    • erch
      erch about 11 years
      @HermannIngjaldsson My main concern is to stick as close to the Unix Philosophy as close as possible; thus I try to crank out the most of each command via it's options. But nevertheless thanks for the link. I'm watching and learning.
    • aderchox
      aderchox over 3 years
      ls -w60 puts the files in three columns for me
  • erch
    erch about 11 years
    I highly value and appreciate the Unix philosophy, but I also wondered of how far the options would get me.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 11 years
    I understand. The parens were more of a side note than a directive.
  • erch
    erch about 11 years
    My bad - I used the lower case option where the upper case should be, as you pointed out correctly. Sorry for that!
  • Michael Durrant
    Michael Durrant about 9 years
    This worked well for me! However you should know that is is about number of CHARACTERS, not columns. Despite this it can basically do the job. ls -w1 means that after the dir is printed if the column position is greater than 1 it will newline, i.e. always. ls -90 on the other hand means let the line width be up to 90 which effectively means two columns.
  • Admin
    Admin almost 8 years
    Good answer since it clearly answered the question but I'm not sure "few would want it to" is on the mark. It looks like there's lots of folks who want the option on occasion. Based on the citation, shouldn't ls drop support for ls -1, too?
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams almost 8 years
    @jww: All -1 does is force ls to use the same format that it would if you were piping its output to another program.
  • taari
    taari over 4 years
    ls -l | cut -d " " -f n where n is the column