2

I'm wondering if there is a way to keep the characteristic color of the extension type in ls --color=auto as well as underlining the file if it is a hard link.

For instance, if I do

 LS_COLORS="*.tgz=01;31:mh=04" ls --color=auto foo.tgz 

I see the file in bold and red, but if I do:

ln foo.tgz bar.tgz
LS_COLORS="*.tgz=01;31:mh=04" ls --color=auto foo.tgz

now I see the file in white and underline. I would like to see it in bold red and underline, and of course, make this work with other extensions too.

0

All hard-linked files can be shown in bold-red by modifying the command

LS_COLORS="*.tgz=01;31:mh=04" ls --color=auto foo.tgz

to

LS_COLORS="*.tgz=01;31:mh=04;01;31" ls --color=auto foo.tgz

The mh= part of the LS_COLORS variable refers to hard-linked files. There is a table in the ls source code which does not appear in the documentation:

enum indicator_no
  {
    C_LEFT, C_RIGHT, C_END, C_RESET, C_NORM, C_FILE, C_DIR, C_LINK,
    C_FIFO, C_SOCK,
    C_BLK, C_CHR, C_MISSING, C_ORPHAN, C_EXEC, C_DOOR, C_SETUID, C_SETGID,
    C_STICKY, C_OTHER_WRITABLE, C_STICKY_OTHER_WRITABLE, C_CAP, C_MULTIHARDLINK,
    C_CLR_TO_EOL
  };

static const char *const indicator_name[]=
  {
    "lc", "rc", "ec", "rs", "no", "fi", "di", "ln", "pi", "so",
    "bd", "cd", "mi", "or", "ex", "do", "su", "sg", "st",
    "ow", "tw", "ca", "mh", "cl", NULL
  };

and the 04 is the SGR code (select graphic rendition) for underline. The 01 and 31 are bold and red, respectively. By adding those to the variable in the part for mh, you can color hard-linked files just like other files (or different, if you choose different numbers).

However, GNU ls chooses only one scheme for coloring each file. The colon : separates schemes. After checking for special categories such as hard-links (and symbolic links and directories), the program only looks for suffixes such as tgz when none of those categories apply. In the source-code, that is commented

  /* Check the file's suffix only if still classified as C_FILE.  */

and once it has selected a scheme, it does just that one, without attempting to combine schemes.

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.