本教程将讨论将文件从特定扩展名更改为另一个扩展名的快速方法。我们将为此使用 shell循环、rename命令。 |
方法一:使用循环
在目录中递归更改文件扩展名的最常见方法是使用 shell 的 for 循环。我们可以使用 shell 脚本提示用户输入目标目录、旧的扩展名和新的扩展名以进行重命名。以下是脚本内容:
[root@localhost ~]# vim rename_file.sh #!/bin/bash echo "Enter the target directory " read target_dir cd $target_dir echo "Enter the file extension to search without a dot" read old_ext echo "Enter the new file extension to rename to without a dot" read new_ext echo "$target_dir, $old_ext, $new_ext" for file in *.$old_ext do mv -v "$file" "${file%.$old_ext}.$new_ext" done;