Unique Linux filename, sortable by time

≡放荡痞女 提交于 2019-12-06 00:08:08
lurker

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

or

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

Which would give you:

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

or

2015-02-23-08-37-xxxxx
# the same as above, but shorter unique string

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.

You will need %N for precision (nanoseconds):

filename=$(date +%s.%N)_$(uuidgen -t); echo $filename
1424699882.086602550_fb575f02-bb63-11e4-ac75-8ca982a9f0aa

BTW if you use %N and you're not using multiple threads, it should be unique enough.

You could take what TIAGO said about %N precision, and combine it with taskset You can find some info here: http://manpages.ubuntu.com/manpages/hardy/man1/taskset.1.html and then run your script

taskset --cpu-list 1 my_script

Never tested this, but, it should run your script only on the first core of your CPU. I'm thinking that if your script runs on your first CPU core, combined with date %N (nanoseconds) + uuidgen there's no way you can get duplicate filenames.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!