PDA

View Full Version : Renaming multiple files in Unix


Nav
31-07-2003, 21:50:19
I need to do a bulk renaming of a number of files

ie names a234234.jpg, b234234.jpg, c2342343.jpg
to x234234.jpg, x234234.jpg, x2342343.jpg

So I only want to change the first character of each file (regardless of the character) to x.

There are over 600 I need to do and I dont particularly want to do each by hand.

Help? :)

Sir Penguin
31-07-2003, 22:23:26
Are there repeats of the numerical part (like a111111.jpg, b111111.jpg, etc.)?

Does each filename consist of a letter, six digits, and .jpg?

SP

Sir Penguin
31-07-2003, 22:31:59
If the answers are no and yes, then the following Perl script might work. I'm not too sure, since I'm in a lab and I can't test it.

It might be a good idea to back up the directory, and I make no guarantees until I get home and test it. :)

#!/usr/bin/perl -w
## file: rename.pl

while (<STDIN>) {
if (/\w(\d{6})\.jpg/) {
`mv $_ newdir/x$1.jpg`
}
}
This would be called like this:

$ ls | ./rename.pl

SP

Darkstar
31-07-2003, 22:41:26
You don't need scripts. A real UNIX weanie would just use rename with single wildcard to whatever they wanted, since all but first character is identical. And if you got the right version, you can even specify for your rename to do a recursive search down your file try to find your source single charcter wildcarded file and rename it.

Why are you always reaching for scripts, black and white grasshopper? The power of the basic UNIX is much vaster then you seem to understand.

Sir Penguin
31-07-2003, 22:48:01
How do you extract the digit group on the shell?

I use scripts when I know how do something using scripts. I use the command line when I know how to do something on the command line. If I know how to do something using both, I either use the easiest one, or the most enjoyable one, or the one which will improve my skills the most, depending on how lazy I'm feeling. I didn't know how to do this on the command line, and I did know (approximately) how to do it with a script, so I suggested a script.

Part of the power of *NIX is the ease with which you can do things with scripts. :)

SP

Darkstar
31-07-2003, 23:06:03
Must be dinner time, as I'm unsure what you meant. Humm...

You don't need a shell script, using rename with a single wildcard.

You can use basic string operations to preserve the latter part of the file name, and specify that as part of your rename. The exact string operations needed to be specified depends on your shell script you are using, of course.

Sir Penguin
31-07-2003, 23:16:53
See, I don't know how to do that. I can't know everything, you know. ;)

How would you do it on your favourite shell?

SP

Sean
31-07-2003, 23:18:10
In Python surely it would be:

(blah)
filename = x + filename[1:]
(blah)

?

No longer Trippin
31-07-2003, 23:32:19
So you keep all your porn on a UNIX system Nav, what's with the renaming x, for x rated? :)

Sir Penguin
01-08-2003, 00:31:43
In Python, I would go#!/usr/bin/python
## file: rename.py

import glob,os

files = glob.glob("./*.jpg")
for f in files:
os.system( "mv %s ./newdir/%s" %(f,'x'+f[3:]) )


Which is a lot simpler than the Perl one, but I wasn't sure of the glob syntax and I couldn't be bothered to open the library reference on the lab computers.

My Perl script is a piece of shit, of course. I guess I was just pissed because I had spent almost two hours in the crappy engineering building computer lab, waiting for the rest of my group to show up to present our final project (they never did). Maybe I just suck. Yeah, that's it. I'd write it like this:#!/usr/bin/perl -w
## file: rename.pl

my $ls = `ls`;
for my $l (split /\n/,$ls) {
if ($l =~ /^\w(\d{6}\.bmp)$/) {
system("mv $l ./newdir/x$1");
}
}SP

Sean
01-08-2003, 00:39:35
Why are you slicing at 3 rather than 1?

Sir Penguin
01-08-2003, 00:48:43
The glob.glob("./*.jpg") bit returns a list with the files in that exact format, so each index is in the form "./a123456.jpg". You'd slice at 1 if you did glob.glob("*.jpg").

SP

Sean
01-08-2003, 01:02:06
Ah, I see. If I’d actually tried it I would have known, of course :).

Darkstar
01-08-2003, 05:02:41
I've been away from *NIX too long. I'd have to do a couple of mans to make sure I got the syntax right, Penguin.

Don't worry, you'll learn it all... in 20 or 30 years. :) There's lots in *NIX, and each flavor has it's own particulars to do lots of common tasks.

Deacon
01-08-2003, 06:57:24
I think there's a bash way to do that, but I'm in Windows right now so I can't read the manpage. Well, make that a sed and bash way. It would be disgusting, but it'd go sorta like this...

for blah in [a-z]*.jpg; do
REN=`echo $blah | sed -e "s/letter before the numbers/z/g"`
mv $blah $REN
done

Check the sed manpage for the correct regexp stuff...

Nav
01-08-2003, 12:00:29
Originally posted by Darkstar
You don't need a shell script, using rename with a single wildcard.

You can use basic string operations to preserve the latter part of the file name, and specify that as part of your rename. The exact string operations needed to be specified depends on your shell script you are using, of course.

That's the kind of thing I need to do. Can you give an example? I'm really a complete novice when faced with unix.

Sir Penguin
01-08-2003, 12:10:09
If you don't want to use a script like the ones I posted, you need to know what shell you're using so that you can use the correct command line syntax. What does it say when you run

echo $SHELL

?

SP

Nav
01-08-2003, 12:12:28
the latter part of the string wouldn't necessarily be the same or the same length.

Nav
01-08-2003, 12:16:27
SP, /bin/bash

Nav
01-08-2003, 12:17:46
man for rename

SYNOPSIS
rename from to file...

DESCRIPTION
rename will rename the specified files by replacing the
first occurrence of from in their name by to.

For example, given the files foo1, ..., foo9, foo10, ...,
foo278, the commands

rename foo foo0 foo?
rename foo foo0 foo??

will turn them into foo001, ..., foo009, foo010, ...,
foo278.

And
rename .htm .html *.htm

will fix the extension of your html files.

Sir Penguin
01-08-2003, 12:22:02
In that case, running the Python script#!/usr/bin/python
## file: rename.py
import glob,os
files = glob.glob("./*.jpg")
for f in files:
os.system( "mv %s %s" %(f,'x'+f[3:]) )in the same directory as the files would work. Make sure it's indented the same way it is above, as Python is picky about indentation.

Most *NIX systems have Python installed these days. Note that if two filenames have the same sequence of digits and differ only by the first letter, one of them will most likely be overwritten.

SP

Nav
01-08-2003, 12:27:19
the files are like this..

-rw-r--r-- 1 admin_ke site4 353380 Dec 15 2002 C5BH01Z00P.jpg
-rw-r--r-- 1 admin_ke site4 81757 Oct 28 2002 C5BH02Z02P.jpg
-rw-r--r-- 1 admin_ke site4 97833 Oct 28 2002 C7AN01Z04Y.jpg
-rw-r--r-- 1 admin_ke site4 147484 Oct 28 2002 C7AN02Z02Y.jpg
-rw-r--r-- 1 admin_ke site4 126435 Nov 2 2002 C7AN03Z07Y.jpg
-rw-r--r-- 1 admin_ke site4 169948 Oct 28 2002 C7BQ01Z06Y.jpg
-rw-r--r-- 1 admin_ke site4 188847 Oct 28 2002 C7BQ02Z08Y.jpg
-rw-r--r-- 1 admin_ke site4 70221 May 4 16:20 H1AA02B25Y53-V2.jpg
-rw-r--r-- 1 admin_ke site4 87506 May 4 16:20 H1AA02B25Y53.jpg
-rw-r--r-- 1 admin_ke site4 80818 Apr 27 09:56 H1AA03B25Y52.jpg
-rw-r--r-- 1 admin_ke site4 60933 Apr 27 09:56 H1AA03B30P52-V2.jpg
-rw-r--r-- 1 admin_ke site4 129980 Jun 28 17:14 H1AS05G25P52.jpg
-rw-r--r-- 1 admin_ke site4 107597 Feb 4 11:45 H1BB02Z60P.jpg
-rw-r--r-- 1 admin_ke site4 111493 Oct 28 2002 H1BL01L12P.jpg

I could do the command a few times If I specify the starting letter to change each time.

something like

rename C X C?

rename H X H?

or should I use * instead

Nav
01-08-2003, 12:33:49
SP, I seem to have python installed. do I just upload that script to the server? how do I run it.

and yeah the file names are still unique even if I change the first letter to the same character.

Sir Penguin
01-08-2003, 12:58:08
Just copy the last [code] stuff that I posted into a text file, and save it (in the same directory as the picture files) as rename.py . There are a couple things you'll have to check:

First off, you have to find the location of the python executable. You do this by running "which python". If the output of that isn't "/usr/bin/python", you have to change the first line of the script to the output. For example, if the output was "/public/bin/python", then the first line of the code should read, "#!/public/bin/python".

Also, the output filename is set in the last line. If you want a capital X at the beginning, change the part of the last line that says, "%(f,'x'+f[3:])" to "%(f,'X'+f[3:]).

Finally, run "chmod 755 rename.py" to set the script to be executable, and run "./rename.py". When it's done, your files should be renamed in the same directory.

SP

Sir Penguin
01-08-2003, 13:00:42
Oh, and if there are any files that have .JPG as their extension (as opposed to .jpg), the script won't catch those. if there are files with .JPG, it's probably easiest to run the script as it is, and then replace the ".jpg" on line 4 with ".JPG", and run it again.

SP

Nav
01-08-2003, 15:29:11
SP, I gather you have tested this?
Shall I trust you. Hmm.. Think I will test it myself first. :D

and is that the command to run it?

run "./rename.py"

Nav
01-08-2003, 16:01:12
told you I'm a unix newbie.. I worked out it was 'exec' no such command (I can see or find) as 'run'.

It worked btw. thanks ;)

Sir Penguin
01-08-2003, 17:07:32
Good! Not 'run' or 'exec', actually. What I meant was that you run it as a local script, which looks like:

./rename.py

. From what I understand, what happens is that the shell looks at the first line of the file, which starts with #! (shebang), executes the program to which the path after the shebang points, and passes the script name as an argument. It's equivalent to running 'python ./rename.py'.

SP

Nav
01-08-2003, 17:23:26
well you should have said that in the first place! I'm not a mind reader ;)

btw I owe you 3p. That is what I would have been paid based on the execution time of the script. Now if I had done it by hand...

Qaj the Fuzzy Love Worm
01-08-2003, 20:17:28
...you would have been insane and we would have been forced to commit you to your nearest mental institution.

Sir Penguin
01-08-2003, 20:25:59
Or a UNIX scripting course.

SP

Deacon
02-08-2003, 05:29:15
We must be careful when we call it Unix scripting, since there are implementations of Python and Perl that run on other OSs. :)

Sir Penguin
02-08-2003, 05:52:48
Well, scripting on UNIX, the results of which may or may not transfer to other platforms.

SP

Qaj the Fuzzy Love Worm
04-08-2003, 21:07:40
But that'd be a stupidly long-ass name for a course.

Sir Penguin
04-08-2003, 21:44:42
They could shorten it down to "Scrpt. UNIX, Res. M/MN Trans. Oth Pltfrms".

SP

Deacon
05-08-2003, 09:19:52
Or something more vague and short like: Advanced Script Writing. :)

Darkstar
05-08-2003, 20:27:35
UNIX Scripting 101