Linux command to generate new GUID?

Since you wanted a random UUID, you want to use Type 4 instead of Type 1:

python -c 'import uuid; print str(uuid.uuid4())'

This Wikipedia article explains the different types of UUIDs. You want "Type 4 (Random)".

I wrote a little Bash function using Python to generate an arbitrary number of Type 4 UUIDs in bulk:

# uuid [count]
#
# Generate type 4 (random) UUID, or [count] type 4 UUIDs.
function uuid()
{
    local count=1
    if [[ ! -z "$1" ]]; then
        if [[ "$1" =~ [^0-9] ]]; then
            echo "Usage: $FUNCNAME [count]" >&2
            return 1
        fi

        count="$1"
    fi

    python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))'
}

If you prefer lowercase, change:

python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))'

To:

python -c 'import uuid; print("\n".join([str(uuid.uuid4()) for x in range('"$count"')]))'

Assuming you don't have uuidgen, you don't need a script:

$ python -c 'import uuid; print(str(uuid.uuid4()))'
b7fedc9e-7f96-11e3-b431-f0def1223c18

You can use command uuidgen. Simply executing uuidgen will give you time-based UUID:

$ uuidgen
18b6f21d-86d0-486e-a2d8-09871e97714e

Tags:

Linux

Guid