Fortunately, it's true, so many others walked before that path so here are some quick tricks for shell programming using the wonderful date UN*X program:
- Converting epoch:
- From epoch to anything else: date -d @$epoch_value +FORMAT (where FORMAT is of course as described on date(1), and '@' makes the actual undocummented trick).
- From anything else to epoch: date +%s
- Calculating times
- One day forward: date -d "1 day"
- One day backwards: date -d "1 day ago"
- Just imagine "1 month", "3 months ago" and the like. Not only google is so friendly with human language ;-)
- More format conversion: '-d' option accepts several other formats as input, even with calculations:
- date -d "1977-08-19 30 years", yeah! my 30th birthday was on sunday. Thanks date, and it was (as epoch): date -d "1977-08-19 30 years" +%s... 1187474400 :-D
- Funny ls:
You can, of course, change the way date is shown. That's because 'YYYY-MM-DD hh:mm' is also a valid input format for date as it is 'YYYY/MM/DD'.
ls -l | while read perms links user group size d t name
do
echo $perms $links $user $group $size $( date -d "$d $t 1 day" ) $name
done
Update: This features and more are indeed described in the coreutils info manual. Thanks mp for pointing it out.