Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename all files in directory from $filename_h to $filename_half?

Tags:

bash

shell

Dead simple.

How do I rename

05_h.png
06_h.png

to

05_half.png
06_half.png

At least, I think it's simple, but it's hard to Google for this kind of thing unless you already know.

Thanks....

like image 811
Richard Avatar asked Sep 16 '11 22:09

Richard


2 Answers

Just use bash, no need to call external commands.

for file in *_h.png
do
  mv "$file" "${file/_h.png/_half.png}"
done

Do not add #!/bin/sh

For those that need that one-liner:

for file in *.png; do mv "$file" "${file/_h.png/_half.png}"; done
like image 370
bash-o-logist Avatar answered Sep 23 '22 02:09

bash-o-logist


Try rename command:

rename 's/_h.png/_half.png/' *.png

Update:

example usage:

create some content

$ mkdir /tmp/foo
$ cd /tmp/foo
$ touch one_h.png two_h.png three_h.png
$ ls 
one_h.png  three_h.png  two_h.png

test solution:

$ rename 's/_h.png/_half.png/' *.png
$ ls
one_half.png  three_half.png  two_half.png
like image 76
Michał Šrajer Avatar answered Sep 23 '22 02:09

Michał Šrajer