Nothing's quite as enraging as not being able to find a simple solution to a simple problem.
November 2, 2011 12:19 PM   Subscribe

Unix wizards: How the heck can I sequentially rename a bunch of files to 001, 002, etc., without using a shell script?

The catch is, I'm not actually using Unix. I do, however, have most of the standard GNU utilities compiled into Win32. I do not have a bash shell.

Frustratingly, I had a simple, working solution to this a couple of months ago and didn't note the command line. I'm almost positive rename was involved (although I could be wrong).

To clarify, I want the files renamed to simple (padded) numbers with no prefixes, in the same order the original filenames were in, without caring what the original filenames were.

I don't particularly care if the sequence starts with 0 or 1. The previous solution I had started with 0. What on earth was it?
posted by neckro23 to Computers & Internet (15 answers total) 3 users marked this as a favorite
 
I'm pretty sure that Métamorphose can do this. It's a free, open source, cross platform batch renamer.
posted by jedicus at 12:23 PM on November 2, 2011


Theres a bunch of solutions here, including a windows batch file

http://stackoverflow.com/questions/5417979/batch-rename-sequential-files-by-padding-with-zeroes
posted by wongcorgi at 12:26 PM on November 2, 2011


Response by poster: To clarify (again), I want a command-line solution to this, using the core Unix utilities, not a Windows solution.
posted by neckro23 at 12:31 PM on November 2, 2011


What shell are you using? PowerShell? cmd?
posted by demiurge at 12:33 PM on November 2, 2011


So you want, like, a Windows batch file that uses Unix mv? I can't figure out what exactly you're looking for, here...
posted by word_virus at 12:35 PM on November 2, 2011 [1 favorite]


"rename" is a windows command, not a unix shell command.
posted by telstar at 12:37 PM on November 2, 2011 [1 favorite]


I think they can run unix utilities at the windows command line.

OP, there's a one-liner using 'rename' at the above stackoverflow link, among other solutions that apparently won't do. This is not the windows rename, but rather (I think) a perl script available on some unixes that takes regexps as its first argument. It isn't a GNU utility, and there is no GNU utility (or standard unix utility) with this name. Is this what you're looking for?
posted by advil at 12:43 PM on November 2, 2011


Here is the documentation for (I believe) the rename command you must have in mind.
posted by advil at 12:45 PM on November 2, 2011


Response by poster: Yes, I have the Unix utilities on the command line, but am using Windows. I'm talking about Unix rename, not the Windows version. I can't use a bash script because I don't have bash.

I had a single-line solution that worked, I'm just trying to find it again.

The rename line on Stackoverflow is:

rename 's/\d+/sprintf("%04d",$&)/e' *.png

This feels pretty close but I can't use sprintf due to being in Windows.
posted by neckro23 at 12:47 PM on November 2, 2011


The solution in either environment is a 'for' loop. I'm just unsure about where Unix needs to enter into it.
posted by word_virus at 12:57 PM on November 2, 2011


Well, there is no unix rename, only that perl script. The use of "sprintf" is actually Perl "sprintf" which I believe should work cross-platform.
posted by advil at 12:59 PM on November 2, 2011


Install Strawberry Perl and use the sprintf. Unless you don't have install capabilities.
posted by caution live frogs at 1:05 PM on November 2, 2011


Best answer: Is awk one of the "standard" tools installed? If so, you could try something like this:

ls | awk '{system(sprintf("mv %s %03d", $0, n++));}'

No idea how this will behave on Win32.
posted by aparrish at 1:17 PM on November 2, 2011


By saying that you're "not looking for a Windows solution," does that mean you aren't interested in ways to do this in pure cmd.exe, i.e. batch or Windows command line. It's got its own stuff, you know, and it might be easier that way.
posted by rhizome at 1:19 PM on November 2, 2011


Best answer: Oh gosh darn it to heck.

It turns out I never did have a working rename solution for this. Instead I was using ImageMagick to generate the filenames sequentially. (I'm trying to manage/convert large numbers of images.)

Thanks for the suggestions, all!
posted by neckro23 at 2:25 PM on November 2, 2011


« Older Help my friend pack better!!   |   What is it actually like going to a well above... Newer »
This thread is closed to new comments.