FOR循环删除文件后缀名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FOR循环删除文件后缀名相关的知识,希望对你有一定的参考价值。

工作目录下有1.txt,2.txt,3.txt...10.txt,10个文件,如下所示:

-rw-r--r-- 1 root root 0 2月  24 14:59 10.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 1.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 2.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 3.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 4.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 5.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 6.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 7.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 8.txt
-rw-r--r-- 1 root root 0 2月  24 14:59 9.txt

需求是将文件的后缀名.txt批量删除,shell脚本如下:

#!/bin/bash
for i in `ls`; do mv -f $i `echo $i | sed ‘s/\.txt//‘`;done

将此shell脚本放到当前的工作目录下,授予可执行权限,运行可实现此功能,实现后的效果如下:

-rw-r--r-- 1 root root  0 2月  24 14:59 1
-rw-r--r-- 1 root root  0 2月  24 14:59 10
-rw-r--r-- 1 root root  0 2月  24 14:59 2
-rw-r--r-- 1 root root  0 2月  24 14:59 3
-rw-r--r-- 1 root root  0 2月  24 14:59 4
-rw-r--r-- 1 root root  0 2月  24 14:59 5
-rw-r--r-- 1 root root  0 2月  24 14:59 6
-rw-r--r-- 1 root root  0 2月  24 14:59 7
-rw-r--r-- 1 root root  0 2月  24 14:59 8
-rw-r--r-- 1 root root  0 2月  24 14:59 9


本文出自 “学无止境” 博客,请务必保留此出处http://linfang.blog.51cto.com/4424588/1744647

以上是关于FOR循环删除文件后缀名的主要内容,如果未能解决你的问题,请参考以下文章