How to add random characters/numbers to a filename? (Solved)

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
RSH
Posts: 127
Joined: Tue May 28, 2024 7:33 pm
Location: Kassel
Has thanked: 59 times
Been thanked: 39 times
Contact:

How to add random characters/numbers to a filename? (Solved)

Post by RSH »

Hi.

During the process of converting files I'm in trouble, as I need to finish one file before starting to convert another one. So, I thought about to add a random part of characters/numbers to the temp filename - each time another and different one automatically.

Is this possible?
How can I do such in shell script?

Last edited by Flash on Thu Oct 31, 2024 3:32 am, edited 1 time in total.
Reason: To make it reflect the subject of the post

My OS: ArtStudio64 - a Woof-CE built from Bionic 18.04
Running in RAM only, no save file, no save folder
www.youtube.com/@RainerSteffenHain

User avatar
MochiMoppel
Posts: 1289
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 476 times

Re: Random parts of a filename, how?

Post by MochiMoppel »

RSH wrote: Wed Oct 30, 2024 12:38 pm

How can I do such in shell script?

You don't need a script. The mktemp tool command does exactly what you are asking for. Run mktemp --help for the syntax.

Last edited by MochiMoppel on Thu Oct 31, 2024 12:40 am, edited 1 time in total.
User avatar
RSH
Posts: 127
Joined: Tue May 28, 2024 7:33 pm
Location: Kassel
Has thanked: 59 times
Been thanked: 39 times
Contact:

Re: Random parts of a filename, how?

Post by RSH »

Hey, that's great.
Never heard of mktemp.

Thanks a lot! :D

My OS: ArtStudio64 - a Woof-CE built from Bionic 18.04
Running in RAM only, no save file, no save folder
www.youtube.com/@RainerSteffenHain

User avatar
fredx181
Posts: 3246
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 407 times
Been thanked: 1409 times
Contact:

Re: Random parts of a filename, how?

Post by fredx181 »

Or if mktemp is not available (could be) something like myfile=myfile-$RANDOM-$RANDOM; touch /tmp/$myfile
Or for directory: mydir=mydir-$RANDOM-$RANDOM; mkdir /tmp/$mydir
edit: probably twice $RANDOM is exaggerated .

Doggy
Posts: 30
Joined: Thu Dec 02, 2021 7:00 pm
Been thanked: 7 times

Re: How to add random characters/numbers to a filename? (Solved)

Post by Doggy »

I ran into a similar issue when I was working on a project that involved file conversions. What worked for me was using a combination of date and $RANDOM in my shell script to create unique temporary filenames. Here’s a simple way to do it: you can set your filename like this: filename="tempfile_$(date +%s)_$RANDOM". This way, each time you run the script, it generates a different name, so you won’t have to deal with overwriting files. If you need something more secure, using mktemp is also a great option to ensure your filenames are unique and avoid conflicts.

User avatar
MochiMoppel
Posts: 1289
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 476 times

Re: How to add random characters/numbers

Post by MochiMoppel »

Sorry for reviving this already solved thread, but since the topic of creating random strings (for passwords, not filenames) came up here, I just want to add that the mentioned mktemp can be used to easily generate random strings. Strings are a combination of alphanumerics [a-zA-Z0-9], so not really suitable for strong passwords, but may be sufficient in other scenarios.

To generate a 6 digit random string try this in a console:

Code: Select all

mktemp -u XXXXXX
Post Reply

Return to “Programming”