Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get large list of files, sorted by file time in *milliseconds*

Tags:

linux

php

php-5.2

I know my file system is storing the file modification time in milliseconds but I don't know of a way to access that information via PHP. When I do an ls --full-time I see this:

-rw-r--r-- 1 nobody nobody 900 2012-06-29 14:08:37.047666435 -0700 file1
-rw-r--r-- 1 nobody nobody 900 2012-06-29 14:08:37.163667038 -0700 file2

I'm assuming that the numbers after the dot are the milliseconds.

So I realize I could just use ls and have it sort by modification time, like this:

$filelist = `ls -t`;

However, the directory sometimes has a massive number of files and I've noticed that ls can be pretty slow in those circumstances.

So instead, I've been using find but it doesn't have a switch for sorting results by modification time. Here's an example of what I'm doing now:

$filelist = `find $dir -type f -printf "%T@ %p\n" | sort -n | awk '{print $2}'`;

And, of course, this doesn't sort down to the milliseconds so files that were created in the same second are sometimes listed in the wrong order.

like image 373
Jeremy Avatar asked Jun 29 '12 23:06

Jeremy


1 Answers

Only a few filesystems (like EXT4) actually store these times up to nanosecond precision. It's not something that's guaranteed to be available, on other filesystems (like EXT3) you'll notice that the fractional part is .000000000

Now, if this feature is really important for you, you could write a specialized PHP extension. This will bypass the calls to external utilities and should be a great deal faster. The process of creating extension is well explained in many places, like here. A reasonable approach to such an extension could be an alternative fstat function implementation that exposes the high-precision fields available in the stat structure defined in /usr/include/bits/stat.h nowadays.

As usual, nothing is free. This extension will have to be maintained, it's probably not possible to get it running on hosted environments, etc. Plus, your php solution will only run on servers where your extension was deployed (although that can circumvented by falling back on the ls based technique if the extension is not detected).

like image 104
fvu Avatar answered Oct 07 '22 15:10

fvu