 |
 |
How to create a randomly named folder in the shell?
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status:
Offline
|
|
Hello,
I'm wondering if/how it is possible to create a randomly named folder (numerical/alpha-numerical, maybe 10 characters) from within the terminal (I'll be using it in a script). The same folder will be later deleted in the script, so ideally I'd like it to be held in some kind of variable. In the script I'll need to "cd" into it and work within it.
I'm reading about "awk" to see if it works...still reading...
Any help is really appreciated...thank you very much!
(Last edited by anothermacguy; Mar 18, 2007 at 02:47 PM.
(Reason:more info))
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
[codex]RANDOMNAME=`ruby -e "puts (1..10).collect { (i = rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join"`
mkdir $RANDOMNAME[/codex]
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status:
Offline
|
|
Thanks ChuckIt - I dont understand that code I'm going to stare at it for a while...
While I was reading I also discovered mktemp, and was able to make a directory using it... how would go about assigning this a variable and using it in a script?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status:
Offline
|
|
ChuckIt - THANKS A LOT - script is working like a dream
That ruby stuff is amazing, though I don't understand it at all (I did it plug and play)... would you mind explaining the syntax a little? Thanks again!
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Sure. It's a slightly obfuscated way to write it to keep it on one line, and the logic behind it relies on knowing ascii. To break it down:
(1..10).collect
Executes the following code 10 times and gathers all the results.
i = rand(62)
There are 62 alphanumeric characters, so we want a random number between 0 and 61.
i += ((i < 10) ? 48
If that randomly generated number is less than 10, we add 48 to get the ascii value for that number (the numbers in ascii are 48 through 57).
((i < 36) ? 55 : 61
If it's more than 10 but less than 36, we instead add 55 to get an uppercase letter (the uppercase letters in ascii are 65 through 90), and if it's more than 36 we add 66 to get a lowercase letter.
The .chr just turns the number we chose into an alphanumeric character.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status:
Offline
|
|
Here's a somewhat simpler option:
dirname=$(mktemp -d /tmp/myprefix-XXXXX)
where you actually type the XXXXX part (the Xs are replaced by random characters when it creates the directory), the -d tells 'mktemp' to make a directory rather than a file, and the variable 'dirname' now contains the path to the directory. Of course, rather then '/tmp/myprefix-', you can use whatever pattern you want.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Originally Posted by Mithras
Here's a somewhat simpler option:
dirname=$(mktemp -d /tmp/myprefix-XXXXX)
where you actually type the XXXXX part (the Xs are replaced by random characters when it creates the directory), the -d tells 'mktemp' to make a directory rather than a file, and the variable 'dirname' now contains the path to the directory. Of course, rather then '/tmp/myprefix-', you can use whatever pattern you want.
Nice one Mithras! I've never heard of the mktemp utility previously, and looking at the man page for it, I could have made use of it in a couple of applications/scripts in the past. Thanks for bringing it to my attention.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Whoa, I didn't know about mktemp either. Nice little tool.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
mktmp is a great find, and my scripts will be using that from now on. Just to provide another option (superfluous at this point), you could also use 'uuidgen' to get a completely random name. That is guaranteed to be completely unique: no other computer should generate that string for something like 5,000 years.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|