1、使用for循环在/oldboy目录下创建安10个文件,名称依次为:
oldboy-1
oldboy-2
..................
oldboy-10
脚本如下:
1 [[email protected] tmp]# cat oldboy.sh 2 #!/bin/sh 3 mkdir -p /tmp/oldboy && cd /tmp/oldboy 4 for n in {1..10} 5 do 6 touch oldboy-$n 7 done
执行结果:
1 [[email protected] tmp]# sh oldboy.sh 2 [[email protected] tmp]# cd oldboy 3 [[email protected] oldboy]# ll 4 total 40 5 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-1 6 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-2 7 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-3 8 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-4 9 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-5 10 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-6 11 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-7 12 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-8 13 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-9 14 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-10
2、将以上文件名中的oldboy全部改成linux
[[email protected] tmp]# cd oldboy
[[email protected] oldboy]# ls
oldboy-1 oldboy-10 oldboy-2 oldboy-3 oldboy-4 oldboy-5 oldboy-6 oldboy-7 oldboy-8 oldboy-9
脚本如下:
法一:
1 [[email protected] tmp]# cat modi.sh 2 #!/bin/sh 3 cd /tmp/oldboy 4 for n in {1..10} 5 do 6 mv linux-$n oldboy-$n 7 done
执行结果:
1 [[email protected] tmp]# sh modi.sh 2 [[email protected] tmp]# cd oldboy 3 [[email protected] oldboy]# ll 4 total 48 5 -rw-r--r-- 1 root root 60 Nov 25 04:34 modi02.sh 6 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-1 7 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-10 8 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-2 9 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-3 10 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-4 11 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-5 12 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-6 13 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-7 14 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-8 15 -rw-r--r-- 1 root root 0 Nov 25 04:18 oldboy-9
法二:
1 [[email protected] tmp]# cat modi-oldboy.sh 2 #!/bin/sh 3 cd /tmp/oldboy 4 for f in `ls oldboy*` 5 do 6 mv $f linux-`echo $f|cut -d"-" -f2` 7 done
执行结果:
1 [[email protected] tmp]# sh modi-oldboy.sh 2 [[email protected] tmp]# cd oldboy/ 3 [[email protected] oldboy]# ls 4 linux-1 linux-10 linux-2 linux-3 linux-4 linux-5 linux-6 linux-7 linux-8 linux-9 modi02.sh
时间: 2024-10-20 17:42:29