18

In my current directory, I execute the command: ls -1 and it gives a list of the current directory contents.

In the same directory, I repeat the command: ls and it gives me the same result, with perhaps a different formatted output.

Finally, I try to find out about the available commands by typing ls --help and the output is:

usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

It looks like the last option is 1 (#1). Can someone explain what the ls -1 does and how it's different to the standard ls command?

Kusalananda
  • 333,661
  • 2
    Related questions are https://unix.stackexchange.com/questions/410550/ , https://unix.stackexchange.com/questions/112125/ , and https://unix.stackexchange.com/questions/111975/ . – JdeBP Jul 19 '18 at 13:45
  • 1
    check if your ls command is also aliased – ron Jul 19 '18 at 13:56
  • I know Suse aliasies ls in /etc/bash.bashrc for example, and also has an environment variable "LS_OPTIONS" and they use some options I do not care for so I manually comment out any aliasing of the ls command; if this is happening under the hood on you without you knowing then ls output can sometimes not make sense and be confusing (i.e. why is this happening) ls -1 should simply output one column but only if some other option is not overriding or preventing it from happening – ron Jul 19 '18 at 14:03
  • 1
    ls -1 is often used by shell scripts to ensure there is minimal information and one file per line to make processing file names easy. – Mark Stewart Jul 19 '18 at 14:20
  • 2
    @Mark indeed, but parsing ls is a bad idea, and ls -1 is the default when the output isn’t a terminal (which is the case when the output is supposed to be processed in a script). – Stephen Kitt Jul 19 '18 at 17:55
  • I agree parsingls is a bad idea, and you are right; forgot about that being the default when STDOUT is not a terminal. – Mark Stewart Jul 19 '18 at 19:10
  • try info ls or man ls if ls --help does not help – axxis Jul 20 '18 at 12:40

3 Answers3

30

Yes, the formatting of the output is the only difference between ls -1 and ls without any options.

From the ls manual on my system:

 -1      (The numeric digit "one".) Force output to be one entry per line.
         This is the default when output is not to a terminal.

This is also a POSIX option to the ls utility.

The manual for ls on your system is bound to say something similar (see man ls).

Related:

Salman A
  • 103
Kusalananda
  • 333,661
9

ls -1 lists one file per line. By default, when it’s outputting to a terminal, ls lists files in columns, whose number varies depending on the length of the file names and the available space on screen.

To find this kind of information, man ls works better than ls --help. (This is true for most commands.)

Stephen Kitt
  • 434,908
0

man ls contains (searched via /-1<Enter>)

   --format=WORD
          across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C

ls -1 triggers the single column mode. ls by default displays its content in multiple columns, just like the column command line tool would.

  • 4
    The user is on a BSD system, judging from what ls --help produces. Their manual would unlikely contain --format. – Kusalananda Jul 19 '18 at 13:51