Script to generate a unique ID based on date

First 14 characters are based on the current time stamp in format YYYYMMDDHHMMSS, followed by an underscore and a random set of four alphanumeric characters. I'm not sure how likely this method is to generate duplicate IDs, but would guess quite unlikely. For a quick test, generated 5K four character sets and saw zero duplicates, so I think the chances of duplicates generated within one second are very low, unless /dev/urandom on your system is not very random.

#!/bin/bash
 
date=$(date +%Y%m%d%H%M%S)
rand=$(cat /dev/urandom | tr -cd [:alnum:] | head -c 4)
ID=$date"_"$rand

Example output:

20101230193212_pOiF

1 Comment

  • 1. Guest replies at 13th July 2013, 2:03 am :

    Thanks!

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).