Skip to content

Instantly share code, notes, and snippets.

@mitry
Created December 1, 2025 04:29
Show Gist options
  • Select an option

  • Save mitry/d8065ef18e41b5a9d31ab3cfe8e559bd to your computer and use it in GitHub Desktop.

Select an option

Save mitry/d8065ef18e41b5a9d31ab3cfe8e559bd to your computer and use it in GitHub Desktop.
#!/usr/bin/awk -f
#
# AWK script to convert dircolors configuration file
# to LS_COLORS environment variable.
#
# Reads a dircolors-style file (e.g., ~/.dir_colors or /etc/DIR_COLORS)
# and outputs an 'export LS_COLORS="..."' command.
#
# Can be used as an alternative to the 'dircolors' utility for `busybox`.
#
# @usage: budybox awk -f dircolors.awk ~/.dircolors
# @usage: eval "$(awk -f /path/to/dircolors.awk ~/.dircolors)"
#
BEGIN {
FS = " ";
map["NORMAL"] = map["NORM"] = "no";
map["RESET"] = "rs"
map["FILE"] = "fi";
map["DIR"] = "di";
map["LINK"] = map["LNK"] = map["SYMLINK"] = "ln";
map["ORPHAN"] = "or";
map["MISSING"] = "mi";
map["PIPE"] = map["FIFO"] = "pi";
map["SOCK"] = "so";
map["DOOR"] = "do";
map["BLOCK"] = map["BLK"] = "bd";
map["CHAR"] = map["CHR"] = "cd";
map["EXEC"] = "ex";
map["MULTIHARDLINK"] = "mh"; # ???
map["SETUID"] = map["SUID"] = "su";
map["SETGID"] = map["SGID"] = "sg";
map["CAPABILITY"] = "ca"; # ???
map["OTHER_WRITABLE"] = map["OWR"] = "ow";
map["STICKY"] = "st";
map["STICKY_OTHER_WRITABLE"] = map["OWT"] = "tw";
# Пропустить эти термы
ignore["TERM"] = 1; # Starts a terminal-specific section and specifies which terminal it applies to.
ignore["COLOR"] = 1; # Slackware only
ignore["COLORTERM"] = 1;
ignore["EIGHTBIT"] = 1; # Slackware only
ignore["OPTIONS"] = 1; # Slackware only
ignore["LEFTCODE"] = ignore["LEFT"] = 1; # non-ISO/IEC 6429 terminals
ignore["RIGHTCODE"] = ignore["RIGHT"] = 1; # non-ISO/IEC 6429 terminals
ignore["ENDCODE"] = ignore["END"] = 1; # non-ISO/IEC 6429 terminals
ls_colors = "";
}
/^[[:space:]]*#/ { next }
/^[[:space:]]*$/ { next }
{
# Удаляем комментарии в конце строки:
# всё после `#`, но не в первом поле
gsub(/[[:space:]]*#.*/, "", $2);
type = $1;
code = $2;
if ($1 == "") next;
if (type in ignore) next;
# Преобразуем тип, если он есть в массиве
if (type in map)
type = map[type];
else if (type ~ /^\./) # Если начинается с точки, добавляем *
type = "*" type;
if (ls_colors == "")
ls_colors = type "=" code;
else
ls_colors = ls_colors ":" type "=" code;
}
END {
print "LS_COLORS='" ls_colors "';\nexport LS_COLORS\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment