I want to rename all the files in a folder so that the new names are
simply the old names prefixed by a 4-5 digit random number (different
random number for each file). I looked at man rand in Terminal and the
description seemed to indicate it would be the piece that could supply
the random numbers. However when I try to invoke rand I get an unknown
command type of error.
I suppose I need to add something to the PATH variable so that rand can
be found. But where is rand located?
Is there an alternative to rand that would work?
TIA
Eric
On 10.4, the only "rand" command I find is actually a feature of
openssl, not a free-standing tool. It also doesn't look like it's a
particularly good way to generate a string of random digits without
significant post-processing. It generates a series of bytes - values
with a range from 0-255. I suppose you could generate 2 bytes and treat
them as a single number in [0,65535].
openssl rand 2 | hexdump -e '1/2 "%05u"'
Uniqueness not guaranteed of course, being that it's random and all.
--
"Harry?" Ron's voice was a mere whisper. "Do you smell something ... burning?"
- Harry Potter and the Odor of the Phoenix
Michelle Steiner wrote:
> In article <484EDB61...@infowest.com>,
> Sandstone <spamb...@infowest.com> wrote:
>
>
>>Is there an alternative to rand that would work?
>
>
> Why not use Applescript's built-in random number generator?
>
> set rand to (random number from 1000 to 99999) as string
>
Well, I was planning on doing this file renaming with a single shell
script in terminal - I'm not sure how to integrate this Applescript
statement into the shell script. Sorry, I'm a newbie with Terminal commands.
Or are you suggesting I do the renaming entirely in Applescript? Another
thing I'd have to learn.
Michelle Steiner wrote:
> In article <484F17B3...@infowest.com>,
> Sandstone <spamb...@infowest.com> wrote:
>
>
>>>Why not use Applescript's built-in random number generator?
>>>
>>>set rand to (random number from 1000 to 99999) as string
>>>
>>
>>Well, I was planning on doing this file renaming with a single shell
>>script in terminal - I'm not sure how to integrate this Applescript
>>statement into the shell script. Sorry, I'm a newbie with Terminal
>>commands.
>>
>>Or are you suggesting I do the renaming entirely in Applescript?
>>Another thing I'd have to learn.
>
>
> I wasn't paying close attention, and thought I was reading the
> applescript newsgroup.
>
> If you would like, I'll show you how to do it all in applescript.
>
I may take you up on this but first I think I may have answered my own
question. Evidently the bash shell supports a variable named RANDOM and
if I type the command
echo $RANDOM
then I get a 4-5 digit random number that is good enough for my purpose.
I've got a USB thumb drive containing 300-400 JPEGs in a folder. I plug
the thumb drive into a digital picture frame but the frame's Shuffle
feature is really lame and I get very little randomness in the
slideshow. So, I thought I'd randomize the JPEG filenames instead and
ignore the Shuffle. Voila - random slideshow.
Is it OK that you run the risk of getting a duplicate that overwrites
the previous?
How about
sum *.jpg | sort -n | while read SUM SIZE NAME; do
cp $NAME <dir>/$SUM.jpg
done
I suppose it's possible for two check sums to be the same.
But I think it's less likely than for two 0-32768 $RANDOMS
to be the same.
To guarantee uniqueness, you could
cp $NAME <dir>/$SUM-$(date '+%S%M').jpg
sleep $(($RANDOM/3200+1))
--
Wes Groleau
There ain't no right wing,
there ain't no left wing.
There's only you and me and we just disagree.
(apologies to Jim Krueger)
An alternative to rand would be uuidgen. It's "guaranteed to be unique
over both space and time"*
However, its output looks like this:
0114CE4D-6B15-489D-8D4D-9C52A2D7C5EE which may not be suitable for
renaming files.
*That bit about space and time reminds me of a tech manual for a zone
plate generator I read yesterday. "T Reset button -- is used to stop time
(as it applies to TSG1001 zone plates, anyway)" Just so there's no
confusion.
Or the time I ran across a NSN listing for a "time dilation unit". I was
really tempted to order one of those.
--
Brian Hughes
temp0806 at hughesvideo dot com
> Mac OS 10.3.9, bash shell
>
> I want to rename all the files in a folder so that the new names are
> simply the old names prefixed by a 4-5 digit random number (different
> random number for each file). I looked at man rand in Terminal and the
> description seemed to indicate it would be the piece that could supply
> the random numbers. However when I try to invoke rand I get an unknown
> command type of error.
Read the synopsis on the man page. The command is 'openssl rand', not
'rand'.
Note that It outputs binary bytes, not human-readable numbers.
Nothing wrong with a file named 96763FDC-376B-11DD-A7A5-003065DD04C0.jpg
But it is not suitable as an alternative to rand.
Observe:
Indigo:~ wgroleau$ for ITEM in *; do
> uuidgen
> done > /tmp/tmp
Indigo:~ wgroleau$ sort /tmp/tmp > /tmp/tmp2
Indigo:~ wgroleau$ sum /tmp/tmp*
34959 6 /tmp/tmp
34959 6 /tmp/tmp2
Indigo:~ wgroleau$ diff /tmp/tmp*
Indigo:~ wgroleau$ wc /tmp/tmp*
164 164 6068 /tmp/tmp
164 164 6068 /tmp/tmp2
328 328 12136 total
In other words, since uuidgen has a constant part and a time-based
part, the values are created in the same order they sort into.
--
Wes Groleau
What kind of smiley is C:\ ?
> *That bit about space and time reminds me of a tech manual for a zone
> plate generator I read yesterday. "T Reset button -- is used to stop time
> (as it applies to TSG1001 zone plates, anyway)" Just so there's no
> confusion.
>
> Or the time I ran across a NSN listing for a "time dilation unit". I was
> really tempted to order one of those.
And "man file" on an old Linux distribution ended in "This software is not
subject to any export provision of the United States Department of
Commerce, and may be exported to any country or planet."
Jochem
--
"A designer knows he has arrived at perfection not when there is no
longer anything to add, but when there is no longer anything to take away."
- Antoine de Saint-Exupery
>
> In other words, since uuidgen has a constant part and a time-based
> part, the values are created in the same order they sort into.
>
Of course we have no idea why the OP wants to do what he wants to do,
so we don't know whether this would be a problem.
My own speculation is that he might reject
0114CE4D-6B15-489D-8D4D-9C52A2D7C5EE as simply too long.
> Is there an alternative to rand that would work?
If you haven't already implemented a solution using $RANDOM, consider
using jot (assuming it was part of 10.3). To get a single 4-5 digit
number, for example, you'd use:
jot -r 1 0000 99999
--
My personal UDP list: 127.0.0.1, 4ax.com, buzzardnews.com, googlegroups.com,
greatnowhere.com, heapnode.com, individual.net, localhost, ntli.net,
teranews.com, vif.com, x-privat.org
> In other words, since uuidgen has a constant part and a time-based
> part, the values are created in the same order they sort into.
>
That doesn't seem to be true here:
odetta:~ mitchell$ for i in 1 2 3 4 5 ; do uuidgen ; done
B669210A-F11E-45F5-9147-70D91415BE16
84EC60C2-DCA3-4D08-BEA8-6D5F85693DBB
099B7C9B-0DAD-4BB7-8693-59167D44C25D
E221B407-0EF9-43AB-9455-DBA48BDFCEF5
0F45B3CA-E76C-4227-A053-6A9931D82DF0
Wes Groleau wrote:
> Sandstone wrote:
>
>> echo $RANDOM
>> then I get a 4-5 digit random number that is good enough for my
>> purpose. I've got a USB thumb drive containing 300-400 JPEGs in a
>> folder. I plug
>
>
> Is it OK that you run the risk of getting a duplicate that overwrites
> the previous?
>
> How about
>
> sum *.jpg | sort -n | while read SUM SIZE NAME; do
>
> cp $NAME <dir>/$SUM.jpg
>
> done
>
> I suppose it's possible for two check sums to be the same.
> But I think it's less likely than for two 0-32768 $RANDOMS
> to be the same.
>
> To guarantee uniqueness, you could
>
> cp $NAME <dir>/$SUM-$(date '+%S%M').jpg
> sleep $(($RANDOM/3200+1))
>
>
All the original filenames are unique. I will be prefixing these
filenames with a random number. If the same random number occurs for two
or more files, the resulting filenames will still be unique.
However, I will copy and save your uniqueness tip - might come in handy
later.
I want to retain the original filenames because they are descriptive and
help me find JPEGs in the folder that are related to a particular
location; thus, filename readability is important. But at the same time
I want them to be prefixed with a short (again, for readability) random
number so that the order they are presented in a digital picture frame
slideshow will be random (this to use in place of the really poor
Shuffle feature of the digital picture frame).
The uniqueness of the random number is not that critical because even if
there are duplicates, the resulting composite filenames will still be
unique. Duplicate random numbers will create a less than perfect
randomness in the slideshow presentation but, with the large number of
JPEGs involved, whi can tell?
Doc O'Leary wrote:
> In article <484EDB61...@infowest.com>,
> Sandstone <spamb...@infowest.com> wrote:
>
>
>>Is there an alternative to rand that would work?
>
>
> If you haven't already implemented a solution using $RANDOM, consider
> using jot (assuming it was part of 10.3). To get a single 4-5 digit
> number, for example, you'd use:
>
> jot -r 1 0000 99999
>
I wasn't aware of jot. I'll see if its available on my 10.3.9 system. I
had decided on RANDOM before you mentioned this.
Yes, I was afraid of that. In general, can binary bytes be sent through
a Unix pipe - and if so, are there commands that could be used at the
other end of that pipe to convert and output the binary as ASCII hex for
example?
The UUID generator changed in 10.4 such that there're no longer
(trivially) predictable or constant elements.
<http://developer.apple.com/technotes/tn/tn1103.html>
"Versions of Mac OS X prior to Mac OS X 10.4 "Tiger" generated version 1
UUIDs, which contain a MAC address. (Tiger generates version 4 UUIDs
which are generated from random numbers. See the Core Foundation release
notes and RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace
for details.)"
The v1 UUID code is still used for code that was build against pre-Tiger
SDKs, in case an application relied on the predictably you reference.
> All the original filenames are unique. I will be prefixing these
> filenames with a random number. If the same random number occurs for two
> or more files, the resulting filenames will still be unique.
Why do you want to prefix unique filenames with a random number which
does not need to be unique? Use the same then at all files, or even
easier, none at all.
Just being curious,
> Sandstone <spamb...@infowest.com> writes:
>
> > All the original filenames are unique. I will be prefixing these
> > filenames with a random number. If the same random number occurs for two
> > or more files, the resulting filenames will still be unique.
>
> Why do you want to prefix unique filenames with a random number which
> does not need to be unique? Use the same then at all files, or even
> easier, none at all.
>
> Just being curious,
>
> Jochem
He wants to provide his own randomization to jpg images so that
the images are displayed in random order.
I assume that he would re-run his randomization script when he
want to create a new random order to the images.
Bob Harris
Try hexdump(1). For example:
$ openssl rand 5| hexdump | head -1 | sed 's/ /-/g' | \
sed -E 's/-+$//' | sed -E 's/^0+-//'
This would output something like '927e-f517-0003'.
--
"If I can't smoke and swear I'm fucked."
marko [dot] vihoma [at] pp1 [dot] inet [dot] fi
Hmm, that commandline can be trimmed to:
$ openssl rand 5|hexdump|head -1| \
sed -E -e 's/ /-/g' -e 's/-+$//' -e 's/^0+-//'
>
> Try hexdump(1). For example:
>
> $ openssl rand 5| hexdump | head -1 | sed 's/ /-/g' | \
> sed -E 's/-+$//' | sed -E 's/^0+-//'
>
> This would output something like '927e-f517-0003'.
>
Jot seems to work much better. For example,
for f in * ; do mv $f `jot -r -w '%04d'$f` 1 0000 9999 ; done
Yes, absolutely. I was just answering the question about converting
bytes from `openssl rand' to something readable. My example isn't the
best of course to create random numbers or strings :)
This is correct.
Also, 100% unique random numbers aren't that necessary since I just want
an acceptable (to me) degree of randomization.
Neat. I'll check to see if jot is available in my 10.3.9 system.
All in all, wouldn't it be a heck of a lot less work to find a
"slideshow" application or utility with an internal option to display
files in random order?
--
Team EM to the rescue! http://www.team-em.com
The problem is that digital photo frames don't run "slideshow"
applications.
--
Tom Stiller
PGP fingerprint = 5108 DDB2 9761 EDE5 E7E3 7BDA 71ED 6496 99C0 C7CF
My digital picture frame has no computer connection other than the USB
thumb drive containing the JPEGs.
Actually, it isn't much work at all since one of the folks who replied
to my question posted a short 1 line terminal command that randomizes
the filenames.
> My digital picture frame has no computer connection other than the USB
> thumb drive containing the JPEGs.
>
> Actually, it isn't much work at all since one of the folks who replied
> to my question posted a short 1 line terminal command that randomizes
> the filenames.
You might want to look at this:
http://redtape.msnbc.com/2008/01/digital-picture.html
--
W. Oates