0

How can ls be flagged to sort .-prefixed hidden directories and files in groups so that its output is sorted as visible directories, hidden directories, visible files, then hidden files?

I currently have ls aliased to ls -lG --color --group-directories-first which groups directories first, but visible and hidden directories are mixed together.

Instead, the output of ls should be:

visibleDirectoryA
visibleDirectoryB
.hiddenDirectoryA
.hiddenDirectoryB
visibleFileA
visibleFileB
.hiddenFileA
.hiddenFileB
  • i can reproduce, with locale en_AU.UTF8. BTW, LANG=C ls .... fails to reproduce (and, in fact, will even sort a directory name like !foo before any of the .hidden directories - strict ascii value sort rather than a natural sort). – cas Jan 13 '18 at 6:19
  • My locale output is LANG=en_US.UTF-8 LANGUAGE= LC_CTYPE=en_US.UTF-8 LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL= – infosmith Jan 13 '18 at 6:56
2

Use -v for natural sort. e.g.

ls -lG --color --group-directories-first -A -v

Note while they are sorted into their own "group", the .hidden directories will appear before the visible directories, not after them, because a . sorts lower than most other characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.