Richard
Richard

Reputation: 32909

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

Upvotes: 204

Views: 266733

Answers (12)

Karoly Horvath
Karoly Horvath

Reputation: 96266

for f in *.png; do
  fnew=`echo $f | sed 's/_h.png/_half.png/'`
  mv $f $fnew
done

Or in one-liner:

for f in *.png; do mv "$f" "$(echo $f | sed 's/_h.png$/_half.png/g')"; done

Upvotes: 16

lucrp
lucrp

Reputation: 582

I had to rename the prefix of files and I found this answer with a solution like this:

for i in h_*; do mv ${i/#h_/half_}; done

If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter.

from man bash

Upvotes: 2

Tharindu Sathischandra
Tharindu Sathischandra

Reputation: 1994

Although the answer set is complete, I need to add another missing one.

for i in *_h.png;
  do name=`echo "$i" | cut -d'_' -f1`
  echo "Executing of name $name" 
  mv "$i" "${name}_half.png"
done

Upvotes: 2

sorpigal
sorpigal

Reputation: 26086

Are you looking for a pure bash solution? There are many approaches, but here's one.

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

This presumes that the only files in the current directory that end in _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.}" ; done

Presuming those two examples are your only. files.

For the general case, file renaming in has been covered before.

Upvotes: 15

James Yen
James Yen

Reputation: 61

I had a similar question: In the manual, it describes rename as

rename [option] expression replacement file

so you can use it in this way

rename _h _half *.png

In the code: '_h' is the expression that you are looking for. '_half' is the pattern that you want to replace with. '*.png' is the range of files that you are looking for your possible target files.

Hope this can help c:

Upvotes: 6

Jadeye
Jadeye

Reputation: 4309

One liner:
for file in *.php ; do mv "$file" "_$file" ; done

Upvotes: 2

Easwar
Easwar

Reputation: 21

Another approach can be manually using batch rename option

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

This will work for linux based gui using WinSCP etc

Upvotes: 2

bash-o-logist
bash-o-logist

Reputation: 6911

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

Upvotes: 406

Michał Šrajer
Michał Šrajer

Reputation: 31182

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

Upvotes: 84

ztank1013
ztank1013

Reputation: 7255

for i in *_h.png ; do
  mv $i `echo "$i"|awk -F'.' '{print $1"alf."$2}'`
done

Upvotes: 5

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

Use the rename utility written in perl. Might be that it is not 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

Upvotes: 8

C. Ramseyer
C. Ramseyer

Reputation: 2382

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

Upvotes: 2

Related Questions