批量重命名文件和文件夹

时间:2011-10-27 17:02:40

标签: bash batch-rename

我对以下问题得到了帮助: batch rename files with ids intact

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

  1. 我在根目录中有一组嵌套文件夹和文件,其中包含[myprefix_foldername]和[myprefix_filename.ext]
  2. 我想将所有文件夹文件重命名为[foldername]和[filename.ext]
  3. 我可以使用与上述帖子中的内容类似的方法吗?

    谢谢! JML

1 个答案:

答案 0 :(得分: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

编辑: IFShttp://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

相关问题