debian:~# ls
1.jpg 2.jpg 3.jpg 4.jpg 5.jpg
debian:~# for file in $(ls);do mv $file `echo $file | sed s/jpg$/png/g`;done
debian:~# ls
1.png 2.png 3.png 4.png 5.png
--------------------
debian:~# ls
1.png 2.png 3.png 4.png 5.png
debian:~# rename png jpg *.png
Bareword "png" not allowed while "strict subs" in use at (eval 1) line 1.
注:这个用法在Debian-base系统中使用会出现上述错误。不过,可以在redhat上使用,在centos中测试可用。在debian中查看man,可知: rename 's/\.bak$//' *.bak
debian:~# ls
1.png 2.png 3.png 4.png 5.png
debian:~# rename 's/.png/.jpg/' * 或 rename 's/png/jpg/' *
debian:~# ls
1.jpg 2.jpg 3.jpg 4.jpg 5.jpg
--------------------
使用find:
debian:~# ls
1.png 2.png 3.png 4.png 5.png
debian:~# find ./ -name "*.png"|while read file; do mv $file $(echo $file|sed s/png/jpg/g); done
或者
debian:~# for file in $(find ./ -name "*.png"); do mv $file $(echo $file|sed s/png/jpg/g); done
debian:~# find . -name "*.png" | awk -F'.' '{print("mv "$0" ."$2".jpg")}'
mv ./4.png ./4.jpg
mv ./5.png ./5.jpg
mv ./2.png ./2.jpg
mv ./3.png ./3.jpg
mv ./1.png ./1.jpg
每一步都命令都打印出,但并没有执行,要执行的话需在后边加个“|sh",即:
debian:~# find . -name "*.png" | awk -F'.' '{print("mv "$0" ."$2".jpg")}' |sh
阅读(914) | 评论(0) | 转发(0) |