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

Dead is easy.

How to rename

05_h.png 06_h.png 

to

 05_half.png 06_half.png 

At least I think it’s easy, but for Google this kind of is difficult if you don’t already know.

Thank....

+69
bash shell
Sep 16 '11 at 22:00
source share
8 answers

Just use bash, no need to invoke external commands.

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

Do not add #!/bin/sh

For those who need one layer:

 for file in *.png; do mv "$file" "${file/_h.png/_half.png}"; done 
+162
Sep 17 '11 at 1:32
source share

Try the rename command:

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

Update:

usage example:

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 
+29
Sep 16 '11 at 10:19
source share

Are you looking for a clean bash solution? There are many approaches, but here is one.

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

This assumes that the only files in the current directory that end with _h.png are the ones you want to rename.

Much more specifically

 for file in 0{5..6}_h.png ; do mv "$file" "${file/_h./_half.}" 

Assuming these two examples are yours. files.

In general, renaming a file to has .

+8
Sep 16 2018-11-11T00:
source share
 for f in *.png; do fnew=`echo $f | sed 's/_h.png/_half.png/'` mv $f $fnew done 
+7
Sep 16 '11 at 10:03
source share

Use the rename utility written in perl. It may not be available by default, though ...

 $ touch 0{5..6}_h.png $ ls 05_h.png 06_h.png $ rename 's/h/half/' *.png $ ls 05_half.png 06_half.png 
+4
Sep 16 '11 at 10:18
source share
 for i in *_h.png ; do mv $i `echo "$i"|awk -F'.' '{print $1"alf."$2}'` done 
+2
Sep 16 2018-11-22T00:
source share

Another approach can be used manually with the option to rename packages

Right-click the file -> Custom File Commands -> Batch Rename and you can replace h. with a half.

This will work with linux-based gui using WinSCP etc.

+1
Sep 21 '17 at 7:59 on
source share

Use the rename utility:

 rc@bvm3:/tmp/foo $ touch 05_h.png 06_h.png rc@bvm3:/tmp/foo $ rename 's/_h/_half/' * rc@bvm3:/tmp/foo $ ls -l total 0 -rw-r--r-- 1 rc rc 0 2011-09-17 00:15 05_half.png -rw-r--r-- 1 rc rc 0 2011-09-17 00:15 06_half.png 
0
Sep 16 '11 at 10:16
source share



All Articles