0

I am new to bash shell programming and need help on this

I have a directory "production\new\images\sp" which contains multiple sub-folders with multiple file name in this format "xxxxx_xxxxx.jpg". I like to rename each of the file name from this format to "xxxxx-xxxxx.jpg" format in each sub-folder.

example:

86193730_43134.jpg =====> 86193730-43134.jpg
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • `find`, `tr`, `mv`. Have you tried something with those 3? – Wrikken Oct 17 '13 at 18:37
  • see [this answer](http://stackoverflow.com/a/602770/1009730) or [this answer](http://stackoverflow.com/a/602840/1009730) – mac Oct 17 '13 at 18:40

3 Answers3

4

Try this :

rename 's/_/-/' *_*.jpg

warning There are other tools with the same name which may or may not be able to do this, so be careful.

If you run the following command (linux)

$ file $(readlink -f $(type -p rename))

and you have a result like

.../rename: Perl script, ASCII text executable

then this seems to be the right tool =)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo update-alternatives --set rename /path/to/rename

(replace /path/to/rename to the path of your perl's rename command.


Last but not least, this tool was originally written by Larry Wall, the Perl's dad.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

In case rename is not available (as on OSX):

find . -type f -name "*.jpg" -execdir bash -c 'f="{}"; nf=$(tr "_" "-" <<< "$f") && mv "$f" "$nf"' \;
anubhava
  • 761,203
  • 64
  • 569
  • 643
-1

this works for me (improved by @sputnik):

for f in *_*.jpg; do mv "$f" "$(echo "$f" | sed 's/_/-/')" ; done
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
tback
  • 11,138
  • 7
  • 47
  • 71