Rename all files in directory from $filename_h to

2019-01-10 00:06发布

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

标签: bash shell
10条回答
太酷不给撩
2楼-- · 2019-01-10 00:25

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
查看更多
Melony?
3楼-- · 2019-01-10 00:28

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

查看更多
你好瞎i
4楼-- · 2019-01-10 00:30

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
查看更多
在下西门庆
5楼-- · 2019-01-10 00:31

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
查看更多
等我变得足够好
6楼-- · 2019-01-10 00:35
for f in *.png; do
  fnew=`echo $f | sed 's/_h.png/_half.png/'`
  mv $f $fnew
done
查看更多
Lonely孤独者°
7楼-- · 2019-01-10 00:38

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
查看更多
登录 后发表回答