views:

1996

answers:

5

There are multiple files in a directory that begin with prefix fgh, for example:

fghfilea
fghfileb
fghfilec

I want to rename all of them to begin with prefix jkl. Is there a single command to do that instead of renaming each file individually?

+16  A: 

There are several ways, but using rename will probably be the easiest.

Using one version of rename:

rename 's/^fgh/jkl/' fgh*

Using another version of rename (same as Judy2K's answer):

rename fgh jkl fgh*

You should check your platform's man page to see which of the above applies.

Stephan202
+1 Didn't even know about rename ... Now I can stop using a for loop with mv and sed ... Thanks!
balpha
Are you sure this will work? Or, did you mix up a couple of streams from your mind on the same line?
nik
Works on my machine (Ubuntu 8.10).
Stephan202
@nik I suppose you're talking to Stephan202 -- Looking at the man page, it really doesn't mention regexes, so you might be right. It still seems to be the right tool: rename fgh jkl fgh* should really do the right thing.
balpha
Whichever way -- rename is the answer.
balpha
Wow. you're right. The man page on my machine is very different from the one I linked... I'll update the answer in a sec. My apologies!
Stephan202
You are linking to a different `rename` then you are showing syntax for http://unixhelp.ed.ac.uk/CGI/man-cgi?rename is the other one
Hasturkun
@Stephan202, now please don't tell me you also have a perl script called rename :-)
nik
Good one Hasterkun, i was expecting something like that tripped Stephan.
nik
@Stephan202: I did not understand `rename` yet. Now, I can have a look at it. Thank you. :-)
Alan Haggai Alavi
@Alan, yes, Stephan's line can actually go into the `rename` man page. He should retain it under his corrected answer.
nik
Thanks all for clarifying the situation! (@nik: it is written in Perl, yes :)
Stephan202
I never came across that before. Definitely non-standard. It's also on RHEL5 but *not* Solaris 10.
Stephen Darlington
Not present on all *nix systems. Not on Max OS X for one, and no package in fink to get it. Haven't looked at MacPorts.
dmckee
AFAICT, `rename` seems to be a Linux specific script or utility. If you care at all about portability, please continue using `sed` and loops or an inline Perl script.
D.Shawley
+4  A: 
rename fgh jkl fgh*
Judy2K
On my machine this produces the error 'Bareword "fgh" not allowed while "strict subs" in use at (eval 1) line 1.'
Stephan202
@Stephan202, what is your machine?
nik
Ubuntu 8.10 (perl v5.10.0 / 2009-06-26)
Stephan202
+7  A: 

This is how sed and mv can be used together to do what Stephan202 is thinking?

for f in fgh*; do mv $f $(echo $f | sed 's/^fgh/jkl/g'); done
nik
Very close. Note that you only want to match the first occurrence of fgh: 's/^fgh/jkl/g' (The caret makes all the difference).
Stephan202
Just for the sake of precision... You mean "fgh at the beginning of the name", not "the first occurrence of fgh". /^fgh/ will match "fghi", but not "efgh".
Dave Sherohman
@Stephan, That was a typo on my part (fixed it).
nik
@Dave: correct. Since only filenames matching fgh* are processed, these notions coincide in this particular case. But indeed I could have been more precise.
Stephan202
Thanks. very useful on my Mac that hasn't got `rename` installed by default. Sed does the trick :)
Jesper Rønn-Jensen
A: 

rename might not be in every system. so if you don't have it, use the shell this example in bash shell

for f in fgh*; do mv "$f" "${f/fgh/xxx}";done
ghostdog74
A: 

Good Article on

Renaming multiple files at once

scripter