linux删除以字母开头的文件,linux下修改以某个字母开头的文件后戳

1、怎么在linux下修改以某一字母开头的文件后戳

源文件内容

[root@localhost test]# ls

stu10.txt.php  stu3.txt.php  stu6.txt.php  stu9.txt.php  test3.txt

stu1.txt.php   stu4.txt.php  stu7.txt.php  test1.txt     test4.txt

stu2.txt.php   stu5.txt.php  stu8.txt.php  test2.txt     test5.txt

现在我们将以s开头的所有文件的后戳修改为.html

第一步:先将以s开头的文件找出来

[root@localhost test]# find -type f -name "s*"

./stu8.txt.php

./stu7.txt.php

./stu6.txt.php

./stu9.txt.php

./stu3.txt.php

./stu4.txt.php

./stu2.txt.php

./stu5.txt.php

./stu1.txt.php

./stu10.txt.php

第二步:取文件的前半部分

[root@localhost test]# find -type f -name "s*"|awk -F"[./]+" '{print $2}'

stu8

stu7

stu6

stu9

stu3

stu4

stu2

stu5

stu1

stu10

第三步:使用拼接的方法来实现文件后戳的修改

[root@localhost test]# find -type f -name "s*"|awk -F"[./]+" '{print "mv "$2".txt.php "$2".html"}'

mv stu8.txt.php stu8.html

mv stu7.txt.php stu7.html

mv stu6.txt.php stu6.html

mv stu9.txt.php stu9.html

mv stu3.txt.php stu3.html

mv stu4.txt.php stu4.html

mv stu2.txt.php stu2.html

mv stu5.txt.php stu5.html

mv stu1.txt.php stu1.html

mv stu10.txt.php stu10.html

第四步:将拼接的内容交给bash来处理

[root@localhost test]# find -type f -name "s*"|awk -F"[./]+" '{print "mv "$2".txt.php "$2".html"}'|bash

第五步:查看修改后的内容

[root@localhost test]# ll

total 0

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu10.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu1.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu2.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu3.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu4.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu5.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu6.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu7.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu8.html

-rw-r--r--. 1 root root 0 Jul 22 11:47 stu9.html

-rw-r--r--. 1 root root 0 Jul 22 11:41 test1.txt

-rw-r--r--. 1 root root 0 Jul 22 11:41 test2.txt

-rw-r--r--. 1 root root 0 Jul 22 11:41 test3.txt

-rw-r--r--. 1 root root 0 Jul 22 11:41 test4.txt

-rw-r--r--. 1 root root 0 Jul 22 11:41 test5.txt

可以看到已经将所有以s开头的文件名后戳全部修改为.html

2、以上修改文件后戳的方法还可以使用脚本来实现

脚本内容如下

#!/bin/bash

workDir=/server/file/test        将目录定义为一个变量

if [ -d $workDir ];then          判断目录是否存在,存在则进入,不存在则推出

cd $workDir

else

exit 1

fi

for i in $(find -type f -name "[s]*")    用for循环来查找以s开头的文件

do

char=`echo $i|awk -F"[/.]+" '{print $2}'`     定义要修改的文件前半部分

mv $i ${char}.html                     将文件修改为想要的内容

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值