If I last modified a file 5 minutes ago, is it possible to make ls-l
output something like "5 mins" instead of the actual date/time?
If I last modified a file 5 minutes ago, is it possible to make ls-l
output something like "5 mins" instead of the actual date/time?
I use stat to get metadata info on the files. Some examples:
Output looks like this:
Then to lookup a single file
and output looks like this:
Otherwise you might need a small bash script to show you the difference between when the file was created and current time.
This is a continuation of an earlier script used for parsing
ls -l
with some enhancement:One way:
You can make
ls
print modification time in seconds since epoch, like so:Then pipe the
ls
output toawk
to do the logic, like so:Because the command line is long, here is a break-up which is basically the same but can be visually more contained:
-v now=$(date +%s)
will assign the current time in seconds since epoch tonow
$6 = now - $6
will calculate the time in seconds since file last modified.d=int($6/60/60/24)
will calculate the days.h=int($6/60/60%24)
will calculate remaining hours less than one day.m=int($6/60%60)
will calculate remaining minutes less than one hour.s=int($6%60)
will calculate remaining seconds less than one minute.$6 = d" Days "h" Hours "m" Mins "s" Secs ago"
will change the modification date to something like 2 Days 5 Hours 3 Mins 47 Secs ago.Another way:
You can construct your own custom
ls -l
alternative in a bash script, like so:or make the output simpler and less time strict, like so:
Notice on usage:
If you save either the above command or the above script in a file in your home directory, name the file
lsl.sh
and make it executable like so:you can add an alias like this:
to your
~/.bashrc
file so you can afterwords run the short command: `from any directory and get the desired result.
Answering my own question here because this is what I actually ended up doing.
I could not get any satisfactory results using anything that parsed the output of
ls -l
into a word stream (bash
,awk
,column
etc) because a) filenames with spaces in messed up, and b)ls -l
's aligned columns messed up (for example, right-aligned filesizes).ls -l | column -t
shows both problems at once.I will get shot down for the inefficiency in this, but this is my eventual script that uses
sed
to replace parts of the output ofls -l
maintaining column width:Sample output of
/var/log/syslog*
:(not sure why log appears here in red; it doesn't on my terminal.)
This is only used from the command line so all those
date
commands don't matter, in fact I don't notice any delay at all.Update:
time
output for `ls -l /var/log/syslog*':time
output for for my new script: