Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Terminal command rand

8 views
Skip to first unread message

Sandstone

unread,
Jun 10, 2008, 3:52:01 PM6/10/08
to
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.

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

Gregory Weston

unread,
Jun 10, 2008, 4:25:11 PM6/10/08
to
In article <484EDB61...@infowest.com>,
Sandstone <spamb...@infowest.com> wrote:

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

Message has been deleted

Sandstone

unread,
Jun 10, 2008, 8:09:23 PM6/10/08
to

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.

Message has been deleted

Sandstone

unread,
Jun 10, 2008, 9:11:25 PM6/10/08
to

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.


Wes Groleau

unread,
Jun 10, 2008, 10:23:53 PM6/10/08
to
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))


--
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)

Brian Hughes

unread,
Jun 10, 2008, 11:51:50 PM6/10/08
to
In article <484EDB61...@infowest.com>,
Sandstone <spamb...@infowest.com> wrote:

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

Wayne C. Morris

unread,
Jun 10, 2008, 11:56:14 PM6/10/08
to
In article <484EDB61...@infowest.com>,
Sandstone <spamb...@infowest.com> wrote:

> 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.

Wes Groleau

unread,
Jun 11, 2008, 12:16:07 AM6/11/08
to
Brian Hughes wrote:
> 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.

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:\ ?

Jochem Huhmann

unread,
Jun 11, 2008, 7:02:27 AM6/11/08
to
Brian Hughes <temp...@hughesvideo.invalid> writes:

> *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

william mitchell

unread,
Jun 11, 2008, 9:00:49 AM6/11/08
to
Wes Groleau <grolea...@freeshell.org> writes:

>
> 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.

Doc O'Leary

unread,
Jun 11, 2008, 10:10:18 AM6/11/08
to
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

--
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

william mitchell

unread,
Jun 11, 2008, 10:31:07 AM6/11/08
to
Wes Groleau <grolea...@freeshell.org> writes:

> 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

Sandstone

unread,
Jun 11, 2008, 11:43:28 AM6/11/08
to

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.

Sandstone

unread,
Jun 11, 2008, 12:04:34 PM6/11/08
to

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?


Sandstone

unread,
Jun 11, 2008, 12:07:09 PM6/11/08
to

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.

Sandstone

unread,
Jun 11, 2008, 12:16:29 PM6/11/08
to

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?


Gregory Weston

unread,
Jun 11, 2008, 3:45:28 PM6/11/08
to
In article <bkI3k.42753$bs3.35150@trnddc07>,
Wes Groleau <grolea...@freeshell.org> wrote:

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.

Jochem Huhmann

unread,
Jun 11, 2008, 7:01:46 PM6/11/08
to
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,

Bob Harris

unread,
Jun 11, 2008, 8:06:22 PM6/11/08
to
In article <m2od67i...@marvin.revier.com>,
Jochem Huhmann <j...@gmx.net> wrote:

> 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

Marko Vihoma

unread,
Jun 12, 2008, 7:37:12 AM6/12/08
to
Sandstone wrote:

>
>
> Wayne C. Morris wrote:
>> Read the synopsis on the man page. The command is 'openssl rand', not
>> 'rand'.
>>
>> Note that It outputs binary bytes, not human-readable numbers.
>
> 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?
>

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

Marko Vihoma

unread,
Jun 12, 2008, 7:50:13 AM6/12/08
to
Marko Vihoma wrote:
> 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'.
>

Hmm, that commandline can be trimmed to:

$ openssl rand 5|hexdump|head -1| \

sed -E -e 's/ /-/g' -e 's/-+$//' -e 's/^0+-//'

william mitchell

unread,
Jun 12, 2008, 9:24:56 AM6/12/08
to
Marko Vihoma <ma...@where.ever.invalid> writes:

>
> 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

Marko Vihoma

unread,
Jun 12, 2008, 9:40:21 AM6/12/08
to

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 :)

Sandstone

unread,
Jun 12, 2008, 7:33:04 PM6/12/08
to

This is correct.

Also, 100% unique random numbers aren't that necessary since I just want
an acceptable (to me) degree of randomization.

Sandstone

unread,
Jun 12, 2008, 7:40:16 PM6/12/08
to

Neat. I'll check to see if jot is available in my 10.3.9 system.


Carl Witthoft

unread,
Jun 14, 2008, 2:04:07 PM6/14/08
to
In article <484FF792...@infowest.com>,
Sandstone <spamb...@infowest.com> wrote:

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

Tom Stiller

unread,
Jun 14, 2008, 2:11:58 PM6/14/08
to
In article <carl-DAD165.1...@comcast.dca.giganews.com>,
Carl Witthoft <ca...@witthoft.com> wrote:

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

Message has been deleted

Sandstone

unread,
Jun 15, 2008, 9:57:17 PM6/15/08
to

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.

Warren Oates

unread,
Jun 16, 2008, 9:28:38 AM6/16/08
to
In article <4855C87D...@infowest.com>,
Sandstone <spamb...@infowest.com> wrote:

> 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

0 new messages