DEV Community

Babu Annamalai
Babu Annamalai

Posted on • Originally published at mysticmind.dev

How to create unique file names in bash shell

For debugging purposes, we had to profile a python program using cProfile which will create a .prof file with the profiled data. Also we wanted to create unique file names for each run using a bash shell.

We had couple of options:

  • Use Unix epoch time which is a number and can be suffixed to a file to make it unique.
  • Use uuidgen to create a GUID. Since it is for dev/debugging purposes, we wanted to shorten it and use only the last part of the GUID. I am quite aware that just using the last part may not be always unique, it is for some dev/debugging so it is okay for our purposes.

We will run through to how to generate a unique file name using both the methods below:

In the below script, we are using $(date +"%s") which provide the current date time as an epoch. The output is something like test-1681287886.prof

PROFILE_FILE=test-$(date +"%s").prof
echo ${PROFILE_FILE}
Enter fullscreen mode Exit fullscreen mode

with the below script, we using a combination of uuidgen and piping it to cut to get the last part of GUID. The output is like test-5F4C3E649454.prof. With regards to the args for cut, -d "-" argument refers to using - as the delimiter and -f5 argument refers to getting the 5th field from the array after splitting it

PROFILE_FILE=test-$(uuidgen | cut -d "-" -f5).prof
echo ${PROFILE_FILE}
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay