 |
 |
Need to batch rename with random name.
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2003
Status:
Offline
|
|
Can anyone tell me how to (from the command line or otherwise) batch rename files with random names being used? I have a series of image stills named 0001.jpg, 0002.jpg, 0003.jpg... etc until 3500.jpg. They are an export from a video, and correspond to the sequence that they were in the video. Ideally, I need the names to be, like a 10-digit-random-number.jpg for what I am trying to do.
Thanks
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status:
Offline
|
|
OK, I'll bite. As a one liner from the terminal, try this:
Code:
ls *.jpg | perl -ne 'chomp;while(-e ($n = (int(rand(10**10-1)).".jpg"))) {;} rename $_, $n;'
Notes:
1) Assumes you want to rename *all* .jpg's in the directory; if not you'll have to modify the "ls *.jpg" to something more specific.
2) Doesn't pad out new file names to 10 digits, so you will get names that vary from 1 digit to 10, though most will be 10 digits.
3) *does* make sure it doesn't ever overwrite any files
Either #1 or #2 could be easily addressed, though at that point the one-liner might get unwieldy, and you'd be better off putting the perl script into a file.
Might be better ways of doing this, but this is pretty simple and quick.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
You might run into a bit of trouble using Neil's solution because the shell tends to barf when fed a long file list. Here's an adaptation of what he wrote above that should get around the problem. I've tested it on a thousand files and it ran OK (in about a second) on my old iMac. Save this to a file called, say "random.pl" (make sure the line endings are unix), change the value of $dirname in the script to whereever you've got these jpg files stored and run the script by entering
perl random.pl
With any luck that should do the job. Let us know if it doesn't work as expected. (Oh yeah, the "sprintf" call will do the zero padding. As per the above disclaimer, it's a bit hacky but should get you where you want to be.)
Cheers,
Paul
Code:
#!/usr/bin/perl -w
use strict;
my $dirname='/Users/pmccann/junk/seq'; #directory containing jpg files
my $n;
chdir $dirname or die "Could not change to $dirname: $!";
opendir DIR,"$dirname" or die "No such dir?: $!";
foreach (readdir(DIR)){
next if (/^\./ or -d $_); #skip directories or dotfiles (including . and ..)
while(-e ($n = sprintf("%014s",(int(rand(10**10-1)).".jpg")))) {;}
rename $_, $n;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Alright, here is my solution. It's not designed to be small, but to work well and be easy to understand.
Code:
#!/usr/bin/env python
import string
def randomizeFile(filepath, newlength):
import random
newname = ""
oldext = string.split(filepath, ".")[-1]
while len(newname) < newlength:
newname = newname + str(int(random.random()*10))
return newname + "." + oldext
def randomizeAllFiles(dir, ext, newlength):
import os
os.chdir(dir)
for file in os.listdir("."):
fileext = string.split(file, ".")[-1]
if ( fileext == ext ):
newname = randomizeFile(file, newlength)
print "Renaming %s to %s" % (file, newname)
os.rename(file, newname)
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
sys.stderr.write("Usage: %s <directory>\n" % sys.argv[0])
raise SystemExit
else:
randomizeAllFiles(sys.argv[1], "jpg", 10)
Save it to "randomize.py" and do chmod +x on the file, then go to town. It has a usage statement to tell you how to use it.
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: New York
Status:
Offline
|
|
Hey I need to do something similar. I have a web site of html files but I am moving my site over to php. Therefore I need to rename all html files to php and I need to add some php code to the beginning of each file. Is there an easy way to do this?
Thanks.
I just remembered that I also need to update all links within files from html to php so I'm basically doing a giant find and replace as well. If anyone could tell me how to do this all that would be awesome. Thanks.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status:
Offline
|
|
You might run into a bit of trouble using Neil's solution because the shell tends to barf when fed a long file list.
What exactly is the problem? I piped the directory contents to the program, no overly long file list. Just wondering if there's something I'm overlooking....
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
Originally posted by neilw:
What exactly is the problem? I piped the directory contents to the program, no overly long file list. Just wondering if there's something I'm overlooking....
Possibly: tcsh runs out of puff eventually. I seem to remember the poster mentioning elsewhere that there might be 10000 files involved. Here it's 3500. In any case, if you try something like
% zsh # My favoured shell. Now make a hunk of files
% touch filename{1..5000}
% touch filename{5001..9000}
% tcsh # the one most people are using
% ls *
/bin/ls: Argument list too long.
That's what I was talking about. It's related to the total length of the string that would be produced by expanding the file list, so you may not hit it if the names are short.
Cheers,
Paul
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status:
Offline
|
|
Originally posted by Paul McCann:
% zsh # My favoured shell.
Don't want to start a shell war or anything but I'm curious as to what other reasons exist, aside the one you demonstrated to use zsh. I think I read somewhere that it was the default shell in 10.3 or was that bash, anyway that doesn't matter, just curious.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status:
Offline
|
|
Originally posted by Paul McCann:
% ls *
/bin/ls: Argument list too long.
Ah, you are quite right of course. I was focusing on the perl, forgot about the ls completely. A fix to my original suggestion, then:
Code:
ls | perl -ne 'if (/.jpg$/i) {chomp;while(-e ($n = (int(rand(10**10-1)).".jpg"))) {;} rename $_, $n;'}
Just having fun with one-liners, though this may be pushing the definition... 
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
Originally posted by waffffffle:
Hey I need to do something similar. I have a web site of html files but I am moving my site over to php. Therefore I need to rename all html files to php and I need to add some php code to the beginning of each file. Is there an easy way to do this?
Thanks.
I just remembered that I also need to update all links within files from html to php so I'm basically doing a giant find and replace as well. If anyone could tell me how to do this all that would be awesome. Thanks.
Sorry it took me so long to answer but my machine has been down all day with disk problems. Anyway, to change the name of all of the .html files to ones with .php suffixes, you can use the following:
find /<Path>/<To>/<Dir> -name "*.html" | sed -e 's/\(.*\).html$/mv "\1.html" "\1.php"/' | sh
and to convert the links, you could use:
find /<Path>/<To>/<Dir> -name "*.html" -exec perl -pi -e 's/(<A [^<>]*HREF="*[^<>"]*)\.html([^<>]*>)/$1.php$2/i' {} \;
Adding lines to files after a matching line is something that sed does very well and I'm pretty sure awk can do it too but I couldn't write something unless I knew what to add and where to add it. However, it shouldn't be that tough to figure out.
|
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
Originally posted by WJMoore:
I'm curious as to what other reasons exist, aside the one you demonstrated to use zsh.
Oh where to begin! zsh has been accused of "feature bloat" because it does so much. I guess that things like it's builtin ftp program would fall into that category and its spell checking mechanism can sometimes be a hassle as well as a help. It has rather elaborate command line editing capabilities, like emac's zap-to-char function. It has the ability to schedule commands and has extended and recursive globbng.
The main feature of zsh is of course its completion mechanism. zsh keeps a large list of functions whose syntax it understands so that it can complete not only filenames and directories but users, hosts and functions themselves. In many cases it can also fill in special syntax chatacters. For instance, type:
scp r<TAB>
to get
scp root@
and then a w and the tab key to get:
scp root@ www.yourhost.com:
assuming www.yourhost.com is in your host list. After that it will complete directories and filenames. Note that the "@" and ":" are included automatically. If there are multiple matches, they are listed the first time the tab key is hit and subsequent hits cycle through the possibilities.
If you are interested in my zsh config scripts, you can find them at zsh.tar.gz. To use them, put the tarball in your home directory and execute:
tar -zxvf zsh.tar.gz
echo "source ~/Library/init/zsh/zshrc" >> ~/.zshrc
This won't overwrite your zshrc but may change it's function if you already have a configuration. To use my configuration scripts by themselves, move your own files out of the way. My files will all be located in a ~/Library/init/zsh directory so it shouldn't overwrite your files. I didn't write that much of it. Most of it was stolen from around from around the web, like at dotfiles.com. However, I did write some and created an arrangement similar to what Wilfredo Sanchez Jr. did for Apple's tcsh. If you find that your own configuration blends nicely with mine to enhance it, please post the results so that I can enjoy it!
|
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
Originally posted by neilw:
ls | perl -ne 'if (/.jpg$/i) {chomp;while(-e ($n = (int(rand(10**10-1)).".jpg"))) {;} rename $_, $n;'}
Why is the semi-colon in braces?
|
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status:
Offline
|
|
Originally posted by Gary Kerbaugh:
Oh where to begin! [snip]
Thanks for the informative reply. I've have decided to give it a try and have set my shell to zsh. I'm in the process of configuring all the options.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
Sorry for the slow reply (and good to hear that you're giving zsh a try; it's a bit of an effort to get things set up how you want, but smooth sailing once you're there).
I was going to write an extensive piece on zsh, but Gary's already made a start. (The tab completion when using scp is a massive timesaver if you're copying files between machines on a regular basis and know how to set up ssh keys properly.)
Some other nice things about zsh? I'm a big fan of using CDPATH and AUTO_CD in parallel. If the latter option is set, and you type something with no arguments which isn't a command, zsh will check to see if it's actually a directory. If it is, the shell will change to that directory. So `bin' on its own is equivalent to `cd bin', as long as the directory `./bin' really exists. This is particularly useful in the form `..', which changes to the parent directory.
CDPATH allows you to specify directories to be checked when searching for file/directory names. Hmm, that sounds rather obscure; let's try and example! If I add my home directory to CDPATH via
cdpath=(~)
then this directory is searched when "cd-ing". So if I have a directory called "src" in my home directory, and I'm lurking in, say, /usr/bin, which *doesn't* contain a directory called "src", then a simple
src
takes me to the ~/src directory (this is using AUTO_CD as well!).
Other interactive goodies include the ability to modify multiline entries "all at once". To see what I mean, paste the following into a window with zsh running:
for (( f = 0; f < 11; f += 1 )); do
print -Pn "%S\r $f %s ";sleep 1
done
Then use the up arrow: you can now edit all three lines in a very intuitive way. Compare with tcsh, where such an operation is a *mess*.
Probably most impressive feature of zsh is its globbing capability, which is best illustrated with a series of examples: I'll assume that "EXTENDED_GLOB" is set in your .zshrc or .zlogin
grep pattern **/*.tex
Finds all occurrences of "pattern" in files in or under the current directory whose name matches *.tex
To give a long listing of all files *a*ccessed in the last 120 *s*econds:
% ls -l *(as-120)
Print the names of all files *m*odified in the last *w*eek:
% print *(mw-1)
For more precision (yeah, right...) add multiple conditions to the braces: all files accessed more than ninety seconds ago but less than
120 seconds ago:
% print *(as-120as+90)
and so on and so forth. Well here's another nice little tidbit: how
much perl code is hanging off this particular directory node? Perl
files typically end with either .pl or .pm, so count it all up:
% wc **/*.{pl,pm}
383 1286 10691 test.pl
49 246 1500 tv.pl
9 18 119 Mate.pm
5 10 101 construction/macperlish.pm
446 1560 12411 total
As a final hairy horror, an example from the
documentation (slightly modified).
How to give a long listing (ie "ls -l" style) of all files in all subdirectories, searching recursively, which have a given name
"filename", case insensitive, are at least 50 KB large, no more than a week old and owned by the root user, and allowing up to a single error
in the spelling of the name. The required expression looks like:
ls -l **/(#ia1)filename(LK+50mw-1u0)
More later, (perhaps something about zmv?)
Paul
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status:
Offline
|
|
Originally posted by Gary Kerbaugh:
Why is the semi-colon in braces?
Delayed answer: no good reason apparently. The semi-colon can be deleted, leaving just the braces.
Nice zsh discussion; I started using it a while ago but haven't really explored its various wonders. This gives me some good ideas.
One thing I can say is that any shell that doesn't support CDPATH is of no interest to me; I got used to that during my ksh-using days and could never go back. Haven't trained myself to use the auto-cd feature of zsh yet though...
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: New York
Status:
Offline
|
|
Originally posted by Gary Kerbaugh:
Sorry it took me so long to answer but my machine has been down all day with disk problems. Anyway, to change the name of all of the .html files to ones with .php suffixes, you can use the following:
find /<Path>/<To>/<Dir> -name "*.html" | sed -e 's/\(.*\).html$/mv "\1.html" "\1.php"/' | sh
and to convert the links, you could use:
find /<Path>/<To>/<Dir> -name "*.html" -exec perl -pi -e 's/(<A [^<>]*HREF="*[^<>"]*)\.html([^<>]*>)/$1.php$2/i' {} \;
Adding lines to files after a matching line is something that sed does very well and I'm pretty sure awk can do it too but I couldn't write something unless I knew what to add and where to add it. However, it shouldn't be that tough to figure out.
Thanks a bunch. I haven't been able to try this out yet because I am not quite ready to make the transition just yet but I will certainly use this to do the task. As for inserting code into each page, what I would basically be doing is one include statement at the top of each page like:
<?php include '/includes/file.php'; ?>
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Dec 2002
Location: someplace
Status:
Offline
|
|
Originally posted by Gary Kerbaugh:
If you are interested in my zsh config scripts, you can find them at zsh.tar.gz. To use them, put the tarball in your home directory and execute:
tar -zxvf zsh.tar.gz
echo "source ~/Library/init/zsh/zshrc" >> ~/.zshrc
This won't overwrite your zshrc but may change it's function if you already have a configuration. To use my configuration scripts by themselves, move your own files out of the way. My files will all be located in a ~/Library/init/zsh directory so it shouldn't overwrite your files. I didn't write that much of it. Most of it was stolen from around from around the web, like at dotfiles.com. However, I did write some and created an arrangement similar to what Wilfredo Sanchez Jr. did for Apple's tcsh. If you find that your own configuration blends nicely with mine to enhance it, please post the results so that I can enjoy it!
Gary,
Why is the shebang line in the prompt file set for fink's zsh?
Code:
gatorparrots% head -1 ~/Library/init/zsh/prompt
#!/sw/bin/zsh
Shouldn't it [generally] be for Apple's default installation of zsh 4.0.4:
Code:
gatorparrots% which zsh
/bin/zsh
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2003
Status:
Offline
|
|
Thanks for contributing everyone, neilw's first suggestion actually fit my need, but I will potentially be trying everyone's advice at one point or another.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
Originally posted by gatorparrots:
Gary,
Why is the shebang line in the prompt file set for fink's zsh?
Ah, let me think ... igneuronce and stupididity? Actually, I originally posted this for someone who had followed my suggestion to install Fink's zsh. Before Jaguar, Apple's version of zsh was 3.0.3 so it was a reasonable suggestion to upgrade. The scripts wouldn't have worked on it anyway. I just missed fixing it when Jaguar came out. I'll fix it next week. Thanks for finding that; if you see anything else, please let me know!
|
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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