79773199

Date: 2025-09-24 01:14:09
Score: 0.5
Natty:
Report link

I had a similar need to handle some long filenames that actually sorted a field embedded near the end of the name. Since the sort key was at the end, it's a lot nicer to have it lined up.

Since, if you want to do something once, you probably want to do it again, here's a shell function that solved the immediate problem:

rjust ()
{
    local line;
    local cols=${COLUMNS:-$(tput cols)};
    while IFS= read -r line; do
        printf "% ${cols}s\n" "${line}";
    done
}  

It is pure shell, unless ${COLUMNS} is not set automatically. You can also override it from the CLI in case you don't want it slammed all the way to the right edge of a wide terminal window:

ls | COLUMNS=80 rjust

In coming up with the above, I also realized that modern versions of column(1) command include in most linux distros can also solve the same problem. Here's the aforementioned filenames sorted by an embedded timestamp and right justified using the abbreviated equivalent of column --table --table-column right:

$ ls *.??? | sort -t. -k4 | column -tC right | head

     PowerCycleTest.SG2v1-sn0314fd-wan-cfg7-man3.0s-atfpy.Sep21221944.csv
         VerboseLog.SG2v1-sn0314fd-wan-cfg7-man3.0s-atfpy.Sep21221944.txt
    VerboseLog.SG2v1-sn0314fe-wan-cfg8-man3.0s-atfpy-100x.Sep22022427.txt
PowerCycleTest.SG2v1-sn0314fd-wan-cfg7-man3.0s-atfpy-10xb.Sep22141040.csv
    RebootTest.SG2v1-sn0314fd-wan-cfg7-man3.0s-atfpy-10xb.Sep22141040.csv
          Test.SG2v1-sn0314fd-wan-cfg7-man3.0s-atfpy-10xb.Sep22141040.csv
    VerboseLog.SG2v1-sn0314fd-wan-cfg7-man3.0s-atfpy-10xb.Sep22141040.txt
PowerCycleTest.SG2v1-sn0314fe-wan-cfg8-man6.0s-atfpy-10xb.Sep23110134.csv
    RebootTest.SG2v1-sn0314fe-wan-cfg8-man6.0s-atfpy-10xb.Sep23110134.csv
Reasons:
  • Blacklisted phrase (1): ???
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: ubergeek