先批量创建文件
#!/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
批量改名
[[email protected] scripts]# cd /tmp/chenyao/ [[email protected] chenyao]# pwd /tmp/chenyao [[email protected] 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 [[email protected] chenyao]# f=chenyao-1.html [[email protected] chenyao]# echo $f chenyao-1.html [[email protected] chenyao]# echo $f|sed ‘s#chenyao\(.*\)html#linux\1php#g‘ linux-1.php [[email protected] chenyao]# mv $f `echo $f|sed ‘s#chenyao\(.*\)html#linux\1php#g‘` //先在命令行中执行命令测试,成功之后再复制到脚本中,这样保险,这是思想 [[email protected] 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 [[email protected] 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 [[email protected] scripts]# sh piliang2.sh [[email protected] 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 [[email protected] scripts]#
老男孩的博客:批量改名
http://oldboy.blog.51cto.com/2561410/711342
时间: 2025-01-14 23:46:25