批量修改文件扩展名脚本

适合在当前目录里,把一批同扩展名文件统一改名。

脚本

#!/bin/bash
 
read -p "请输入要替换的旧扩展名 (例如 txt): " oldext
read -p "请输入新的目标扩展名 (例如 md): " newext
 
shopt -s nullglob
files=(*.$oldext)
 
if [ ${#files[@]} -eq 0 ]; then
  echo "当前目录下没有找到 .$oldext 扩展名的文件。"
  exit 0
fi
 
for file in *.$oldext; do
  name="${file%.*}"
  echo "正在修改: $file ====> $name.$newext"
  mv "$file" "$name.$newext"
done
 
echo "批量修改完成!"

边界

这份脚本只处理当前目录,不会递归进入子目录。