15

I'm running this shell command to get the most recent 20 PHP files modified on my server.

find . -name '*.php' -printf '%TY-%Tm-%Td %TH:%TM:%TS %Tz %p\n' | sort -r | head -20

The output is like this:

2016-08-08 01:44:45.3820716170 -0700 ./html/index.php
2016-08-07 05:39:29.0000000000 -0700 ./html/thumb.php
2016-08-07 03:01:44.0000000000 -0700 ./html/paths.php
...

According to strftime man page, I should just be able to use %S (and %TS)

%S

The second as a decimal number (range 00 to 60). (The range is up to 60 to allow for occasional leap seconds.)

There is probably something going on with find, but the other time parameters work as expected. I'm trying to get results like the following. If the extra string is in fact fractional seconds, either rounding or truncating is fine. How can this be done?

2016-08-08 01:44:45 -0700 ./html/index.php
2016-08-07 05:39:29 -0700 ./html/thumb.php
2016-08-07 03:01:44 -0700 ./html/paths.php
...

(and if there is a more efficient way to do what the above command does, I'm all ears.)

3
  • My find man page says that S includes fractional seconds: "S Second (00.00 .. 61.00). There is a fractional part." and I don't note an option right off that doesn't Aug 8, 2016 at 12:56
  • You might find some helpful suggestions at stackoverflow.com/questions/5566310/… Aug 8, 2016 at 12:59
  • @EricRenouf Nice find on a better recursive find (forgive the pun). It would still be nice to remove the fractional seconds if even possible.
    – Drakes
    Aug 8, 2016 at 13:10

1 Answer 1

21

You can truncate the fractional part using the %.n formatting syntax, where n is an integer specifying the length of the string you want to keep. In your particular case, the incantation would be:

find . -name '*.php' -printf '%TY-%Tm-%Td %TH:%TM:%.2TS %Tz %p\n' | sort -r | head -20

2
  • This works wonderfully. Even %.12TT works. What I can't find is where this is documented. It it is clearly more capable than just this. I put the period in the wrong spot and found it does right justification too. I'd like to learn more, but where? I found nothing here man7.org/linux/man-pages/man1/find.1.html or man7.org/linux/man-pages/man3/strftime.3.html and the characters in this syntax are all wrong for printf to be the place to look...
    – bielawski
    Jan 4 at 22:55
  • My bad. It does actually mention this. "Field widths and precisions can be specified as with the printf(3) C function. "
    – bielawski
    Jan 5 at 0:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .