Does ‘ls’ ignore Zsh glob sorting qualifiers

macoszsh

I try to understand how to use Zsh glob qualifiers with ls.

An article on the internet says that to sort the files from largest to smallest, I can use ls *(oL).

  • o<sort> – Sort files depending on the value of <sort>
  • O<sort> – Like o, but sort in descending order

The value of <sort> can be:

  • n – Sort by name (the default).
  • L – Sort by size.
  • l – Sort by number of links.
  • a – Sort by last access.
  • m – Sort by last modification.
  • c – Sort by last inode change.
  • d – Files in subdirectories appear before.
  • N – Don’t sort anything.

For example:

# Sort files from the smallest to the largest
ls *(oL)

But ls *(oL) doesn't list files in this way for me, ls *(On) doesn't list files in reverse alphabetical order, ls *(om) doesn't sort files by modification date (and there is no difference if you run ls *(Om)). All the commands list files in regular alphabetical order, from A to Z, and that's it. It seems ls ignores Zsh globs?

I should say that in other situations Zsh globs seems to work correctly for me. For example, print -rC1 *(.) prints only files, without directories.

zsh 5.9 (x86_64-apple-darwin23.0)

Best Answer

ls does its own sorting. To disable that, and display the files as they are given (by the glob expansion), use -f on macOS:

ls -f *(oL)

To list directories without listing their contents, add -d. For single-column output, add -1.