1

我得到了关于以下问题的帮助: batch rename files with ids received

这是如何重命名组中特定文件的一个很好的示例,但我想知道是否有类似的脚本可以用来执行以下操作:

  1. 我在包含 [myprefix_foldername] 和 [myprefix_filename.ext] 的根目录中有一组嵌套文件夹和文件
  2. 我想将所有文件夹文件重命名为 [foldername] 和 [filename.ext]

我可以使用与上面帖子中的方法类似的方法吗?

谢谢!jml

4

1 回答 1

1

是的,很容易,用find

find rootDir -name "myprefix_*"

rootDir这将为您提供以 .开头的所有文件和文件夹的列表myprefix_。从那里,可以快速跳转到批量重命名:

find rootDir -name "myprefix_*" | while read f
do
  echo "Moving $f to ${f/myprefix_/}"
  mv "$f" "${f/myprefix_/}"
done

编辑: IFS根据http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html添加

编辑 2: IFS删除以支持while read.

编辑 3:正如 bos 所指出的,如果您的 Bash 版本仍然不喜欢它,您可能需要更改while read f为。while read -d $'\n' f

于 2011-10-27T17:18:35.883 回答