实验素材:
[root@sv7 test]# ls //此目录下包含全是 .txt结尾的文件,要求全部换成 .doc结尾
1.txt 2.txt 3.txt 4.txt 5.txt
编写脚本
[root@sv7 test]# vim /root/25.sh
脚本内容:
#!/bin/bash
#定义i的取值范围,只要是以$1结尾的文件都显示
#对i执行去尾的操作,并加上新的文件后缀
#ls *.$1
要用反引号引起来
for i in ls *.$1
do
mv $i ${ i%.*}.$2 &> /dev/null
done
赋予执行权限
[root@sv7 test]# chmod +x /root/25.sh
测试脚本:
[root@sv7 test]# /root/25.sh txt doc
[root@sv7 test]# ls
1.doc 2.doc 3.doc 4.doc 5.doc
转载于:https://blog.51cto.com/13450962/2064391