I'm trying to figure out how to list the contents of a directory in the following group order:

  1. Hidden directories
  2. Non-hidden directories
  3. All other hidden files (including regular files, symlinks, sockets)
  4. All other non-hidden files (including regular files, symlinks, sockets)

Each group should be sorted A-Z, preferably case-insensitive.

Based on this answer I tried the following:

ls -dlU .*/ */

This gets me half-way there by listing hidden directories and then non-hidden directories. The problem is it doesn't list any files.

I've also tried various ways of piping the output to sort with no luck.

How can I do this?

  • 2
    You could try with LC_COLLATE=C ls -lA --group-directories-first or LANG=POSIX ls -lA --group-directories-first. Though it works on my system (en-US.UTF-8), it sorts case-sensitive and would probably have some strange effects on systems with different locales/foreign characters. – don_crissti Jan 3 '15 at 21:06
  • @don_crissti nice. Any way to get it to ignore case? – Big McLargeHuge Jan 4 '15 at 2:42
  • I guess you could create your own locale definition (based on C but case insensitive or based on en_US but dot-sensitive). Yeah, I know this is half a year later but I stumbled upon this question while looking for some similar stuff... – don_crissti Aug 8 '15 at 0:20

The straightforward way :

find . -maxdepth 1 -type d -name '.*'
find . -maxdepth 1 -type d \! -name '.*'
find . -maxdepth 1 -type f -name '.*'
find . -maxdepth 1 -type f \! -name '.*'
  • 1
    +1. I would like to note that this omits anything that is neither a regular file nor a directory (e.g. symlink, socket), but I guess that is what the OP wants. – Celada Jan 3 '15 at 22:07
  • 1
    I should have mentioned I'd like for the entries to be sorted alphabetically still - hidden dirs A-Z, normal dirs A-Z, etc. - and I didn't mean to omit symlinks completely. Probably just need to replace -type f with \! -type d on those last two lines. – Big McLargeHuge Jan 4 '15 at 2:41
  • 1
    Perhaps with find .. -print0 | sort -z, assuming sort supports the -z option. – muru Jan 4 '15 at 3:13
  • @muru that finds everything under the parent directory recursively. – Big McLargeHuge Jan 4 '15 at 4:23
  • @Koveras I didn't mean the two dots as a path. It was supposed to be three dots (an ellipsis). – muru Jan 4 '15 at 5:40

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.