0

How can I display file timestamps in a relative format, such as "2 minutes ago", using ls, ncdu, tree or a similar tool when listing files in a folder in a Bash shell on Ubuntu?

1
  • touch -t 205012250800 Xmas.2050 may be interesting to deal with. Commented Oct 27 at 8:32

1 Answer 1

4

Not at all. These tools don't support that. But it would be a good place to exercise your coding skills a bit! Or you could try other tools.

  • if you're OK with C and aren't afraid of legacy code bases, adding such functionality to GNU coreutils' ls implementation. You might learn a lot of unexpected things about time stamps and C library functions for time delta handling; you probably want to extend the --time-style option to include something like eza (see below)¹.
  • if you're OK with relatively non-standard replacement for standard tools² :
    • eza --time-style relative is like a more colorful ls with the time formatting you want.
    • lsd --date relative -l is a lot like eza above. (lsd is available in many distros as package, eza not so much currently.)
    • lsd --date relative -l --tree is a bit like a tree invocation. Personally, it's "too much" for me.

¹ caveat: real-world time difference calculations can be somewhat complicated, and maintainers will be hesitant to merge a solution that shows the right time, unless it's on the day of a DST transition, goes across a February 29th… but it's manageable! Don't let yourself be stopped by complexity. I found the coreutils maintainers to be very helpful people; you just need to ask interestedly enough after trying a bit :) A good strategy might be to say "let me cover all the easy cases, and for times long in the past and for the hard cases just display the actual date/time". I suspect the calculations might not be that hard here, because most operating systems deliver file modification times etc in epoch-referenced times, but especially in GNU coreutils, there's need to be compatible with a bunch of different operating system, not all of them very sane. But frankly, things like "hide behind configure-time feature flag and disable on platforms where file times aren't some continuous clock" might be negotiable!

² the icons in these tools get better if your terminal emulator uses a font with an extended set of icons, like the nerdfonts; I personally like "RecMonoLinear Nerd Font".

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.