工作目录下有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