Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to create a randomly named folder in the shell?

How to create a randomly named folder in the shell?
Thread Tools
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status: Offline
Reply With Quote
Mar 18, 2007, 02:35 PM
 
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
Reply With Quote
Mar 18, 2007, 03:09 PM
 
[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
Reply With Quote
Mar 18, 2007, 03:21 PM
 
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
Reply With Quote
Mar 18, 2007, 03:24 PM
 
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
Reply With Quote
Mar 18, 2007, 04:05 PM
 
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
Reply With Quote
Mar 18, 2007, 06:42 PM
 
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
Reply With Quote
Mar 18, 2007, 08:13 PM
 
Originally Posted by Mithras View Post
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
Reply With Quote
Mar 19, 2007, 12:34 AM
 
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
Reply With Quote
Mar 29, 2007, 01:20 PM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:48 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2