Why does ls -x not produce one entry per line per the man page documentation? [closed]

StackOverflow https://stackoverflow.com/questions/21939434

  •  14-10-2022
  •  | 
  •  

Question

I am trying to feed the results of ls into a Perl script. The man page for ls indicates that using the -x switch, ls should output one entry per line. However, it would seem that ls and ls -x produce exactly the same output. Now if I do ls -1, I get the desired output. So, what does the -x switch actually do?

Was it helpful?

Solution

I don't know where you read that -x lists one file per line, but that is wrong. The man page says -x lists files by lines instead of by columns (i.e. two consecutive files will be displayed in two consecutive lines instead of two consecutive columns).

The option you're looking for is -1.

OTHER TIPS

It's display in line not one entry per line

ls display
aaaaaaaaa ccccccccc eeeeeeeeee ggggggggg
bbbbbbbbb ddddddddd ffffffffff hhhhhhhhh

ls - x display
aaaaaaaaa bbbbbbbbbb ccccccccc ddddddddd
eeeeeeeee ffffffffff ggggggggg hhhhhhhhh

According to : man ls

   *    List entries in multiple columns by specifying either the -C or -x flag. The -C flag is the default format when output is to a tty. The ls command displays single
        column output if file or directory names are too long.

so in fact, -x is another way of saying -C, which tells ls to output several files per columns.

You probably want:

ls -1

But if you are trying to feed a list of files to another command, do NOT use ls.

have a look at : http://mywiki.wooledge.org/BashPitfalls (item #1, but read also the whole thing, very informative)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top