【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 ~]#