Sort files to the split second

I need a similar sort, I need to sort the directory by time, but time needs to be to the second or smaller.

Using ls -lt but that is only to the minute.

If you have stat on your system, try:

stat --print='%Y %n\n' *

This will give the last modification time in seconds since the epoch and the name of the file in question. You should be able to do a numerical sort on the first field.

Andrew

Do you mean that you want the seconds displayed in the listing? Using ls -lt will sort by seconds, but they won't display by default.

$ touch -mt 201801010000.00 /tmp/unix/third
$ touch -mt 201801010000.01 /tmp/unix/first
$ touch -mt 201801010000.02 /tmp/unix/second
$ touch -mt 201801010000.03 /tmp/unix/fourth
$
$ ls -l /tmp/unix
total 0
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 first
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 fourth
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 second
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 third
$ ls -lt /tmp/unix
total 0
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 fourth
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 second
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 first
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 third
$

If you need to know the value of seconds, you would be better using the stat command for each file and extracting the part you want, e.g. stat -t /tmp/unix/* | sort -nk 13

This assumes that there are no spaces in the files/paths but might help. The thirteenth column is the file modification time (i.e. edit of the file content) which I'm guessing you might want more than other details. The value is in seconds from Epoch (1970-01-01 00:00:00) but you can use stat options to force the output into a format suitable for you.

I hope that this helps. If I've got it all wrong, please describe what you need.

Kind regards,
Robin

It is not; the sorting is done to the split second although mayhap the time stamp listed is only to the minute. I touch ed a few files within a second and used the --time-style long option to ls (which might not be available on your system):

ls -lat --time-style="+%F %T %N" ?
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 023427295 g
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 023427295 d
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 023427295 b
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 027427049 c
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 027427049 a
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 031426802 i
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 031426802 e
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 035426556 j
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 035426556 h
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 035426556 f
1 Like