4

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?

3
  • 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. Commented Jan 3, 2015 at 21:06
  • @don_crissti nice. Any way to get it to ignore case? Commented Jan 4, 2015 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... Commented Aug 8, 2015 at 0:20

1 Answer 1

1

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 '.*'
5
  • 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
    Commented Jan 3, 2015 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. Commented Jan 4, 2015 at 2:41
  • 1
    Perhaps with find .. -print0 | sort -z, assuming sort supports the -z option.
    – muru
    Commented Jan 4, 2015 at 3:13
  • @muru that finds everything under the parent directory recursively. Commented Jan 4, 2015 at 4:23
  • @Koveras I didn't mean the two dots as a path. It was supposed to be three dots (an ellipsis).
    – muru
    Commented Jan 4, 2015 at 5:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .