2

With regexes you can put a capturing group and refer to it in your action (i.e. \1). Is there something similar for when glob is used in bash?

Say I have files named 'file1', 'file2', 'file3', and I want to rename them to 'foo1', 'foo2', 'foo3'. I'd like to do something like this:

mv file(?) foo\1

is that possible?

initlaunch
  • 467
  • 5
  • 15

2 Answers2

3

You're looking for batch renaming. There are a lot of solutions here on stackoverflow. Here is one example

Community
  • 1
  • 1
james
  • 832
  • 5
  • 7
1

One solution for this particular problem:

for i in `ls | egrep "^file[0-9]?$"`; do mv $i ${i/file/ foo}; done
  • Thanks, similar to the link in the marked answer, and 'ls | egrep PATTERN' is useful when you need the power of regex to list the files you want to affect. – initlaunch Dec 01 '11 at 19:07