Richard Richard - 4 years ago 198
Bash Question

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

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....

Answer Source

Just use bash, no need to call external commands.

for file in *.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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download