在 Bash 中按创建时间到毫秒对文件进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3814290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Order files by creation time to the millisecond in Bash
提问by RikSaunderson
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 exactcorrect 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.
查看修改时间,但它总是显示小时:分钟:秒.00000000000。
Is there a way to do this? Thanks, Rik
有没有办法做到这一点?谢谢,里克
采纳答案by Holstebroe
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.
准确性取决于您使用的文件系统,但即使使用高精度文件系统(如 ext4),stat 的标准实现也使用具有 1 秒分辨率的 time_t。
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.
如果您有权访问吐出所有这些文件的程序源,请尝试将时间戳设置为文件名的一部分,然后按文件名而不是修改时间排序。
回答by poundifdef
I'm not sure this is possible. My reasoning:
我不确定这是可能的。我的推理:
If you look at the stat()function call, you see that it returns a struct containing information about a file. One of its members is this:
如果查看stat()函数调用,您会看到它返回一个包含文件信息的结构体。它的成员之一是这样的:
time_t st_mtime; /* time of last modification */
And if you look at the time_t structure, well, wikipedia says this:
如果您查看 time_t 结构,那么维基百科是这样说的:
Unix and POSIX-compliant systems implement time_t as an integer or real-floating type (typically a 32- or 64-bit integer) which represents the number of seconds since the start of the Unix epoch...
Unix 和 POSIX 兼容系统将 time_t 实现为整数或实数浮点类型(通常为 32 位或 64 位整数),表示自 Unix 纪元开始以来的秒数......
Which means that stat()'s time is in terms of seconds, not milliseconds. I haven't looked at how each inode stores file information, but it might not store info up to the millisecond.
这意味着 stat() 的时间以秒为单位,而不是毫秒。我还没有研究过每个 inode 是如何存储文件信息的,但它可能不会存储长达毫秒的信息。
An alternative might be to append the mill/microsecond value to the filename itself when they are being created and order them that way?
另一种方法可能是在创建文件名时将 Mill/microsecond 值附加到文件名本身并以这种方式订购它们?

