试图重命名一堆文件。
我可以用以下命令在当前目录中将所有foo替换为bar:
ls . | awk '{print("mv "$1" "$1)}' | sed 's/foo/bar/2' | /bin/sh
我该添加什么来使它递归?
编辑/我的解决方案
我不懂这个shell类型的东西,所以我用了一些(相当肮脏的)Ruby来完成:
5.times do
Dir["**/*"].each do |f|
file_name = File.absolute_path f
should_rename = file_name.include? "yolo"
new_file_name = file_name.gsub("yolo", "#{@project_name}")
File.rename(f, new_file_name) if (should_rename and File.exists? f)
end
end
find . -name "*.java" -exec rename 's|\.java|\.txt|' {} +
- Noam Ben Arirename
can be used with a glob too, for examplerename "s/.html/.twig/gi" templates/**/*
- Matt Fletcherfind . -exec rename 's|foo|bar|g' {} +
- Marcel Steinbach