Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order files by creation time to the millisecond in Bash

Tags:

date

file

bash

I need to create a list of files which are located on my hard disk in order of when they arrived on the hard disk. To do so, I have used the following:

ls -lat

which lists all the files in date/time order, however, it only orders them to the nearest second. The problem here is that there are thousands of files and every so often, a few of them come clumped together in the same second. I need the exact correct ordering. I'm guessing the easiest way to do this is to get the creation time to the milli (or perhaps nano) second. To do this, I have tried using the following:

stat $myfile

to look at the modification time, but it always shows hour:minute:second.00000000000.

Is there a way to do this? Thanks, Rik

like image 448
RikSaunderson Avatar asked Sep 28 '10 15:09

RikSaunderson


People also ask

How do I print milliseconds in Shell?

date +"%T. %3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.

How can I find out when a file was created?

Windows file propertiesRight-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.

How do I change file creation date and time in Linux?

touch -r oldfile newfile # sets the modification time of newfile to that of oldfile. Code: touch -r oldfile newfile # sets the modification time of newfile to that of oldfile.

What is creation time?

The date and time that the object was created.


2 Answers

The accuracy depends on the file system you are using, but even with a high accuracy file system such as ext4, the standard implementation of stat uses time_t which has a 1 second resolution.

If you have access to the source of the program spitting out all those files, try setting a timestamp as part of the filename instead and then sort on the filename rather than the modification time.

like image 198
Holstebroe Avatar answered Nov 15 '22 04:11

Holstebroe


you'll probably have to write your own stat command, using the stat(2) function

like image 24
KevinDTimm Avatar answered Nov 15 '22 05:11

KevinDTimm