2

commands ls and ls -C seem to produce the same results - display by columns.

Is there any difference?

Andrejs
  • 141

2 Answers2

10

Referring to info coreutils 'ls invocation':

-C' --format=vertical'

List files in columns, sorted vertically. This is the default for ls if standard output is a terminal. It is always the default for the dir program. GNU ls uses variable width columns to display as many files as possible in the fewest lines.

TL;DR: no, no difference - that's default.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 1
    if standard output is a terminal ... now compare ls > ls.txt and ls -C > lsC.txt – R.M. Oct 31 '15 at 16:56
  • 1
    @R.M. if your point is to say that ls > ls.txt and ls -C > lsC.txt output just one column to a file . . . well , you're right ! The single column ( or -1 flag ) is default output for file, which is what's happening with ls > ls.txt. With ls -C > lsC.txt you're missing something - the width , -w or --width=COLS flag. When in terminal , width is automatically determined from the widths of your screen, but how does ls know width of a file ? It doesnt ! Try this: ls -C -w 300 > lsC2.txt – Sergiy Kolodyazhnyy Oct 31 '15 at 17:32
  • @Serg, you actually answered another question I had in mind (why text in some files overflows on low res screen). Thanks man. – Andrejs Oct 31 '15 at 18:11
  • 1
    @Serg Interesting ... on my machine ls -C > lsC.txt makes multiple column output (for a ~80 character width, it looks like), whereas ls > ls.txt is a single column. -- But my main point was that the '-C' option is not superfluous, and has a purpose when redirecting the output (i.e. when the output is not a terminal), and that "no, no difference" between with/without -C only applies to certain situations. – R.M. Oct 31 '15 at 20:01
  • @R.M. what's your system and ls --version , by the way ? – Sergiy Kolodyazhnyy Oct 31 '15 at 20:28
  • 1
    @Serg - Xubuntu 14.04; ls (GNU coreutils) 8.21; I just checked, and I see the same behavior on CentOS 6.6 with ls version 8.4. – R.M. Oct 31 '15 at 22:08
0

read the output of alias your ls command is in probably doing ls -CF. It may be set in your bash_rc file too. so ls -F will also give the same output.

but you could change it, in which case, you might want it. OR you could use it in a script, and maybe you want that style of output.

j0h
  • 14,825
  • Apropos: http://askubuntu.com/questions/525231/how-can-i-run-original-command-that-aliased-with-same-name (Assuming bash) –  Oct 31 '15 at 12:59
  • alias ls='ls --color=auto' only in my Ubuntu 14.04. Apparently it's just system default as Serg said above. – Andrejs Oct 31 '15 at 14:23