Perl and bash examples
So, all these are good examples but need a little touchup for them to run properly.
The first bash example will only work if you enumerate the files. Simply putting
The 2nd bash example has the problem, at least on my system, that extraneous spaces are added to the beginning and end of the od example. To illustrate, the following code
generates
Obviously, this won't work with rename (mv). So you can strip all the spaces with tr:
So the final script could be:
Finally, a perl script posted by jeremias at http://ask.metafilter.com/78113/How-to-create-a-unique-osx-automator-action-that-randomly-renames-files works almost as posted. Allowing one to supply the directory as a command line argument and removing file/directory handle barewords, the following script works as he intended:
Kudos to the three authors for doing most of the work!!
So, all these are good examples but need a little touchup for them to run properly.
The first bash example will only work if you enumerate the files. Simply putting
won't do anything. The correct example would be:for i in *.jpg
Code:
for i in `ls *.jpg`; do mv $i $RANDOM.jpg; done
The 2nd bash example has the problem, at least on my system, that extraneous spaces are added to the beginning and end of the od example. To illustrate, the following code
Code:
echo "'`od -An -N2 -i /dev/random`'"
generates
'(14spaces)-1606221824(48spaces)'
Obviously, this won't work with rename (mv). So you can strip all the spaces with tr:
Code:
echo "'`od -An -N2 -i /dev/random | tr -d " "`'"
So the final script could be:
Code:
for i in `ls *.jpg`; do mv $i `od -An -N2 -i /dev/random | tr -d " "`.jpg; done
Finally, a perl script posted by jeremias at http://ask.metafilter.com/78113/How-to-create-a-unique-osx-automator-action-that-randomly-renames-files works almost as posted. Allowing one to supply the directory as a command line argument and removing file/directory handle barewords, the following script works as he intended:
Code:
#!/usr/bin/perl -w
#
my $path = shift || die "Directory name must be supplied as an argument!\n";
opendir (my $DIR, $path);
my @files = grep { /\.jpg/i } readdir($DIR); # get list of all jpegs in that directory
@files = sort {(rand 2) <=> 1} @files; # randomize it
for ($i = 0; $i < scalar @files ; $i++) {
$old = $files[$i];
$new = $old;
$new =~ s#^\d*__##; # remove earlier modifications, so you can run this twice
# prepend a random number and two underscores to the filename
$new = $i . "__" . $new;
print "renaming $old to $new\n";
rename("$path/$old", "$path/$new") or warn("Couldn't rename $old to $new"); # rename the file
}
Kudos to the three authors for doing most of the work!!