1

I've looked around for a one line solution ( as bash offers ) to replace part of filename.

Given that a folder has image sequence like

ve2_sq021_sc001_v000.0101.jpg
ve2_sq021_sc001_v000.0102.jpg
ve2_sq021_sc001_v000.0103.jpg
ve2_sq021_sc001_v000.0104.jpg

Need to replace only v000 with v09 ( say ). How is it possible (throughout directory).

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
nish
  • 293
  • 1
  • 3
  • 12

2 Answers2

3

If you have the unix command rename installed you can do this trivially like this:

$ rename v000 v09 *.jpg

$ ls -1
ve2_sq021_sc001_v09.0101.jpg
ve2_sq021_sc001_v09.0102.jpg
ve2_sq021_sc001_v09.0103.jpg
ve2_sq021_sc001_v09.0104.jpg

NOTE: This is using the rename implementation that's included with the package util-linux.

slm
  • 369,824
1
for f in $(ls ve2*); do mv $f $(echo $f | sed s/v000/v09/g ); done

If you want to make it recursive, you could use find instead of ls ve2*

  • 1
    Do not ever parse the output of ls. You'll be sorry for it the instant you encounter a filename or path containing whitespace. Instead, use the globbing of you shell (for f in ve2*; do) or find's -exec parameter. – n.st Feb 11 '14 at 13:51
  • 1
    Also, there's no need to spawn a sed instance for each file to be renamed. Either use rename or at least let bash handle the strong replacement: ${f//v000/v09} evaluates to the value of f with all occurrences of 'v000' replaced by 'v09'. – n.st Feb 11 '14 at 14:00