独特的Linux文件名,按时间排序 [英] Unique Linux filename, sortable by time

查看:203
本文介绍了独特的Linux文件名,按时间排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

previously我使用的uuidgen 创建唯一的文件名,我则需要通过日期/时间通过bash脚本来迭代。因为我已经发现,简单地遍历说通过文件'ls -l命令是不够的,因为显然我只能相信操作系统保持时间戳分辨率秒(nonoseconds是全零通过统计在这个特殊的文件系统和内核浏览文件时, )

Previously I was using uuidgen to create unique filenames that I then need to iterate over by date/time via a bash script. I've since found that simply looping over said files via 'ls -l' will not suffice because evidently I can only trust the OS to keep timestamp resolution in seconds (nonoseconds is all zero when viewing files via stat on this particular filesystem and kernel)

所以,我再但也许我可以只使用类似日期+%S%N 我的文件名。这将打印秒,自1970年以来后跟当前纳秒。

So I then though maybe I could just use something like date +%s%N for my filename. This will print the seconds since 1970 followed by the current nanoseconds.

我可能在工程在这一点上这一点,但这些都是在高使用率的企业系统生成的文件,所以我真的不希望单纯的信任的(诚然非常小)的机会两个文件的时间戳纳秒在同一纳秒生成的,我们得到的碰撞。

I'm possibly over-engineering this at this point, but these are files generated on high-usage enterprise systems so I don't really want to simply trust the nanosecond timestamp on the (admittedly very small) chance two files are generated in the same nanosecond and we get a collision.

我相信的uuidgen脚本有烤来处理这种情况的发生,所​​以它仍然保证在这种情况下,(纠正我,如果我错了独特的有逻辑......我读到什么地方,我认为,但Google的失败是我现在)。

I believe the uuidgen script has logic baked in to handle this occurrence so it's still guaranteed to be unique in that case (correct me if I'm wrong there... I read that someplace I think but the googles are failing me right now).

所以...我正在考虑类似

So... I'm considering something like

FILENAME=`date +%s`-`uuidgen -t`
echo $FILENAME

要确保我创建一个唯一的文件名然后可以用一个简单的'ls'的,谁的名字可以信任迭代都可以通过独特的时间和顺序。

to ensure I create a unique filename that can then be iterated over with a simple 'ls' and who's name can be trusted to both be unique and sequential by time.

更好的想法或缺陷与这个方向?

Any better ideas or flaws with this direction?

推荐答案

如果您订购逐年您的日期格式,月(补零),日(补零),小时(补零),分(补零)那么你可以很容易地时间排序:

If you order your date format by year, month (zero padded), day (zero padded), hour (zero padded), minute (zero padded), then you can sort by time easily:

FILENAME=`date '+%Y-%m-%d-%H-%M'`-`uuidgen -t`
echo $FILENAME

这将使你:

2015-02-23-08-37-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

您可以选择其他分隔符的日期/时间,除了 - 如你所愿,只要他们对Linux的文件名中的有效字符内

You can choose other delimiters for the date/time besides - as you wish, as long as they're within the valid characters for Linux file name.

这篇关于独特的Linux文件名,按时间排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆