先批量创建文件
#!/bin/sh if [ ! -d /tmp/chenyao ] then mkdir /tmp/chenyao -p fi cd /tmp/chenyao for i in {1..10} do touch chenyao-$i.html done
批量改名
[root@lamp scripts]# cd /tmp/chenyao/ [root@lamp chenyao]# pwd /tmp/chenyao [root@lamp chenyao]# ll total 0 -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-1.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-10.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-2.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-3.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-4.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-5.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-6.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-7.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-8.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-9.html [root@lamp chenyao]# f=chenyao-1.html [root@lamp chenyao]# echo $f chenyao-1.html [root@lamp chenyao]# echo $f|sed ‘s#chenyao\(.*\)html#linux\1php#g‘ linux-1.php [root@lamp chenyao]# mv $f `echo $f|sed ‘s#chenyao\(.*\)html#linux\1php#g‘` //先在命令行中执行命令测试,成功之后再复制到脚本中,这样保险,这是思想 [root@lamp chenyao]# ll total 0 -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-10.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-2.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-3.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-4.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-5.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-6.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-7.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-8.html -rw-r--r-- 1 root root 0 Nov 23 22:09 chenyao-9.html -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-1.php [root@lamp chenyao]# vim piliang2.sh //写实现批量改名的脚本 #!/bin/sh cd /tmp/chenyao for f in `ls /tmp/chenyao/` do mv $f `echo $f|sed ‘s#chenyao\(.*\)html#linux\1php#g‘` &>/dev/null done [root@lamp scripts]# sh piliang2.sh [root@lamp scripts]# ll /tmp/chenyao/ //可以看到已经成功改名 total 0 -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-1.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-10.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-2.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-3.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-4.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-5.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-6.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-7.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-8.php -rw-r--r-- 1 root root 0 Nov 23 22:09 linux-9.php [root@lamp scripts]#
老男孩的博客:批量改名
http://oldboy.blog.51cto.com/2561410/711342
原文:http://www.cnblogs.com/along1226/p/4989931.html