ls -l –group-directories-first (act on symlinks as well)

ls option --group-directories-first causes directories to be listed on the top, which makes the output of ls nice and clean:

ls -l --group-directories-first

However, it does not act on symlinks, which are actually symlinks to directories. There is a possibility to use

ls -l -L --group-directories-first

which will list both kind of directories on top, but will not distinguish between proper directory and symlinked directory, which is again confusing.

Can ls display symlinked directories on top, while still keeping them distinct from regular directories?

EDIT:
I am using bash.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

No, but if using zsh, you could do:

mll() {
  (($#)) || set -- *(N-/) *(N^-/)
  (($#)) && ls -ldU -- <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="153155">[email protected]</a>
}

You could also define a globbing sort order like:

dir1st() { [[ -d $REPLY ]] && REPLY=1-$REPLY || REPLY=2-$REPLY;}

and use it like:

ls -ldU -- *(o+dir1st)

That way, you can use it for other commands than ls or with ls with different options, or for different patterns like:

ls -ldU -- .*(o+dir1st) # to list the hidden files and dirs

or:

ls -ldU -- ^*[[:lower:]]*(o+dir1st) # to list the all-uppercase files and dirs

If you have to use bash, the equivalent would be like:

mll() (
  if (($# == 0)); then
    dirs=() others=()
    shopt -s nullglob
    for f in *; do
      if [[ -d $f ]]; then
        dirs+=("$f")
      else
        others+=("$f")
      fi
    done
    set -- "${dirs[@]}" "${others[@]}"
  fi
  (($#)) && exec ls -ldU -- "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="193d59">[email protected]</a>"
)

bash doesn’t have globbing qualifiers or any way to affect the sort order of globs, or any way to turn nullglob on a per-glob basis, or have local context for options (other than starting a subshell, hence the () instead of {} above) AFAIK.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x