4

In ls output, I'd like to group symlinks that point to directories along with the rest of the directories, but still have a way of telling that it's a symlink either with color or an appended symbol. Is there a way to do this?

If a symlink points to a directory, you can tell in output from ls by using the -F, --classify argument and by setting LINK target in the dircolors. Then it will be shown as dir_link@, but with the same color as a directory.

The ls command can group directories together with the --group-directories-first option.

The only way I can see to group symlinks to directories along with other directories is to use the -L, --dereference option. But this changes both the appended indicator as well as the color of the entry (even if you set links to be displayed as their own color independent of the target); it's then indistinguishable from a regular directory.

I'm using GNU ls on Linux.

3
  • Doesn't the ls colors entry allow you to color it differently? Commented Sep 26, 2015 at 5:44
  • This isn't exactly what you are looking for, but I use ls -al (often aliased as ll) and when it displays symlinks, it lists them like directoryname -> path/to/linked/location. it usually colors valid links one color, and broken links in red. Commented Sep 26, 2015 at 6:20
  • @FrankThomas I'm hoping for something that will work with any output option, not just the long listing. But if you add the --dereference option in order to get the directory links sorted next to the directories, then it even stops showing the target information in the long output! Commented Sep 26, 2015 at 6:43

2 Answers 2

2

Finally, as of GNU coreutils v8.31 in 2019,

ls --group-directories-first will also group symlinks to directories.

(Copied from https://unix.stackexchange.com/a/704864/528311)

1

The LS_COLORS variable has an option for symbolic links. The LN option, if given a unique color compared to the DI option, will show all symbolic links as that color instead.

See http://linux-sxs.org/housekeeping/lscolors.html for basic options. There is ways of setting individual extension types to their own colors, so there may be a way to set symbolic link directories a different color than symbolic link files, but that takes playing around.

2
  • 2
    The problem is that to get the link sorted with the directories, you (I think) have to use the --dereference option, which then makes the entry use the DI option instead of the LN option in LS_COLORS. Commented Sep 26, 2015 at 6:35
  • @ohspite you tested it? Drat. Commented Sep 26, 2015 at 7:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.