How to get rid of fractional seconds in find using -printf?

7

1

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.)

Drakes

Posted 2016-08-08T12:34:46.823

Reputation: 237

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 – Eric Renouf – 2016-08-08T12:56:09.253

You might find some helpful suggestions at http://stackoverflow.com/questions/5566310/how-to-recursively-find-and-list-the-latest-modified-files-in-a-directory-with-s

– Eric Renouf – 2016-08-08T12:59:03.533

@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 – 2016-08-08T13:10:49.547

Answers

9

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

Larssend

Posted 2016-08-08T12:34:46.823

Reputation: 2 941