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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用以下命令来实现: ``` sed -i '/^[a-zA-Z]/d' filename ``` 这会删除字母开头的所有行,并且将更改保存回原始文件中。请注意,在执行此命令之前,请务必备份文件以防止数据丢失。 ### 回答2: 在Linux中,可以使用文本处理工具sed来删除以特定字母开头的行,并且覆盖原来的文件。以下是一个示例命令: ``` sed -i '/^字母/d' 文件名 ``` 其中,^表示以特定字母开头,/d表示删除匹配到的行,-i表示直接修改文件。 例如,假设我们有一个文件名为example.txt,内容如下: ``` A line 1 B line 2 C line 3 D line 4 E line 5 ``` 如果我们想删除字母A开头的行并覆盖原文件,可以运行以下命令: ``` sed -i '/^A/d' example.txt ``` 运行后,example.txt的内容将变为: ``` B line 2 C line 3 D line 4 E line 5 ``` 这样就实现了删除字母开头的行并覆盖原文件的操作。 ### 回答3: 要删除字母开头的行并覆盖原来文件,可以使用sed命令。 可以使用以下命令来实现: ```shell sed -i '/^[a-zA-Z]/d' 文件名 ``` 其中的 `-i` 选项表示直接在原文件修改,并且 `/^[a-zA-Z]/d` 是一个正则表达式,表示删除字母开头的行。 例如,假设我们有一个名为 `example.txt` 的文件,其内容如下: ``` 1. ABC 2. 123 3. DEF 4. 456 ``` 执行以下命令: ```shell sed -i '/^[a-zA-Z]/d' example.txt ``` 运行后,`example.txt` 文件的内容将变为: ``` 2. 123 4. 456 ``` 以字母开头的行(1. ABC和3. DEF)已被删除,并且新的内容已经覆盖了原来的文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值