当前位置:首页>微思动态 > >详情
全国热线电话 400-881-4699

在线留言

【Linux 专题】在 bash 中批量修改文件后缀名

发布作者:微思网络   发布时间:2022-09-26   浏览量:0

图片


在目录下有如下文件,现在需要将这 10 个 文件批量加上后缀名 .txt

[root@localhost ~]# touch file{0..9}
[root@localhost ~]# ls
file0  file1  file2  file3  file4  file5  file6  file7  file8  file9
[root@localhost ~]#


Solution

使用以下 for 循环语句即可完成

for i in file*; do mv "$i" "${i%}.txt"done



效果

[root@localhost ~]# \
for i in file*;
do
> mv "$i" "${i%}.txt";
done
[root@localhost ~]# ls
file0.txt  file2.txt  file4.txt  file6.txt  file8.txt
file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
[root@localhost ~]#


如果需要批量去除这些后缀名,可以执行

[root@localhost ~]# ls
file0.txt  file2.txt  file4.txt  file6.txt  file8.txt
file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
[root@localhost ~]# \
for i in *.txt;
do
> mv "${i}" "${i%.txt}";
done
[root@localhost ~]# ls
file0  file1  file2  file3  file4  file5  file6  file7  file8  file9
[root@localhost ~]#



返回顶部