tags:

views:

164

answers:

4

I have a lot of files that have a shared pattern in their name that I would like to remove. For example I have the files, "a_file000.tga" and "another_file000.tga". I would like to do an operation on those files that would remove the pattern "000" from their names resulting in the new names, "a_file.tga" and "another_file.tga".

+3  A: 

Bash can do sed-like substitutions:

for file in *; do mv "${file}" "${file/000/}"; done
Dennis Williamson
+3  A: 

Try this (this works in plain old Bourne sh as well):

for i in *000.tga
do
    mv $i `echo $i | sed 's/000//'`
done
mouviciel
+1 for most portable solution (and because I'm running too low on upvotes to keep upvoting _all_ the good answers).
Chris Lutz
+2  A: 

A non-bash solution, since I know two speedy posters have already covered that:

There's an excellent short perl program called rename which is installed by default on some systems (others have a less useful rename program). It lets you use perl regex for your renaming, e.g:

rename 's/000//' *000*.tga
Jefromi
+3  A: 
#!/bin/bash
ls | while read name; do
  echo mv $name ${name/$1//}
done
DigitalRoss
+1: I love the `| while read` construct.
Jefromi
How is "ls | while read name" better than "for name in *"?
Laurence Gonsalves
@Laurence Gonsalves: using shell wildcards expand the command line, and there's a not-so big limit there. "while read" avoids that
Javier
@Laurence: This is also much more extensible. It's commonly used with `find` on the front.
Jefromi