Skip to content

Instantly share code, notes, and snippets.

@tvidal-net
Last active May 14, 2022 17:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvidal-net/67a00476dc495db96efd1606123c4196 to your computer and use it in GitHub Desktop.
Save tvidal-net/67a00476dc495db96efd1606123c4196 to your computer and use it in GitHub Desktop.
A simple awk script to add color and file-type emoji to `ls -l`
#!/usr/bin/awk -f
# Usage:
# - create a function like the one below and add it to your .rc file
# lsx () { ls -l --file-type --color=always $* | awk -f /path/to/ls.awk; }
# emoji reference
# https://apps.timwhitlock.info/emoji/tables/unicode
BEGIN {
FPAT="[[:space:]]*[^[:space:]]+"
OFS=""
}
function color(i, c) {
printf "%s", "\x1B[" c "m" $i
}
NR > 1 {
switch (substr($1, 0, 1)) {
# directories
case "d":
$9=" \xF0\x9F\x93\x82" $9
break;
# links
case "l":
$9=" \xF0\x9F\x94\x97" $9
break;
# devices
case "b":
case "c":
$5=$5 $6
for (i=6; i<NF; i++) {
$i=$(i+1);
}
$NF=""
$9=" \xF0\x9F\x94\xA7" $9
break;
# regular files
default:
$9=" \xF0\x9F\x94\xB9" $9
break;
}
color(1, "1;30"); # permissions
color(5, "1;33"); # size
color(3, "38;5;027"); # user
color(4, "38;5;061"); # group
color(6, "38;5;219"); # date/time
color(7, "38;5;219"); # date/time
color(8, "38;5;219"); # date/time
color(9);
for (i = 10; i <= NF; i++) {
printf "%s", $i;
}
print "";
next;
}
/^total/ {
color(1, "1;30");
color(2, "33");
print "";
next;
}
{ print; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment