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?
- touch -t 205012250800 Xmas.2050 may be interesting to deal with.Paul_Pedant– Paul_Pedant2025-10-27 08:32:42 +00:00Commented Oct 27 at 8:32
1 Answer
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'
lsimplementation. 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-styleoption to include something likeeza(see below)¹. - if you're OK with relatively non-standard replacement for standard tools² :
eza --time-style relativeis like a more colorfullswith the time formatting you want.lsd --date relative -lis a lot likeezaabove. (lsdis available in many distros as package,ezanot so much currently.)lsd --date relative -l --treeis a bit like atreeinvocation. 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".