获取文件和目录列表:
要对文件进行操作递归地使用find
与球体相比是更好的解决方案。我建议在开始对它们进行操作之前用文件名填充bash数组。此外,我认为一次做这一步,目录然后文件,将是谨慎的。您不想重命名目录,然后在重命名文件时稍后会发现该文件不存在。由于相同的原因,脚本在文件系统层次结构上逐步深入地工作也很重要(因此使用下面的sort
)。
操作上列出:
一旦你的列表,你可以调用一个共同的外壳函数来完成文件或目录名正火和重命名。请注意正确引用名称以获得您想要的内容的重要性。这是非常重要的,因为bash(或者任何shell)在解析命令行时使用空格作为字边界。
脚本:
下面的脚本(名为./rename_spaces.bash
在下面的示例输出)应该做你想要什么。要添加自己的怪异字符,请将它们添加到weirdchars
变量中。请注意,您需要转义字符(,例如单引号已被转义)。如果新文件名存在,脚本会跳过并显示一条消息。这也意味着它将打印消息以进行不重要的重命名(文件名中没有原始名称中的怪异字符)。这可以是恼人的一些人(例如我:-P)
#!/bin/bash
# set -o xtrace # uncomment for debugging
declare weirdchars=" &\'"
function normalise_and_rename() {
declare -a list=("${!1}")
for fileordir in "${list[@]}";
do
newname="${fileordir//[${weirdchars}]/_}"
[[ ! -a "$newname" ]] && \
mv "$fileordir" "$newname" || \
echo "Skipping existing file, $newname."
done
}
declare -a dirs files
while IFS= read -r -d '' dir; do
dirs+=("$dir")
done < <(find -type d -print0 | sort -z)
normalise_and_rename dirs[@]
while IFS= read -r -d '' file; do
files+=("$file")
done < <(find -type f -print0 | sort -z)
normalise_and_rename files[@]
这里是与目录和文件之前和运行上面的脚本后有名称空间目录树的样本输出。
$ tree
.
├── dir1
│ ├── subdir1\ with\ spaces
│ │ └── file1
│ └── subdir2\ with\ spaces&weird\ chars'
│ └── file2
├── dir2
│ ├── subdir1\ with\ spaces
│ │ └── file1\ with\ space
│ └── subdir2\ with\ spaces
│ └── file2\ with\ space
├── dir3
│ ├── subdir1
│ │ └── file1
│ ├── subdir2
│ │ └── file2
│ └── subdir3
│ └── file3
└── rename_spaces.bash
10 directories, 8 files
$ ./rename_spaces.bash
$ tree
.
├── dir1
│ ├── subdir1_with_spaces
│ │ └── file1
│ └── subdir2_with_spaces_weird_chars_
│ └── file2
├── dir2
│ ├── subdir1_with_spaces
│ │ └── file1_with_space
│ └── subdir2_with_spaces
│ └── file2_with_space
├── dir3
│ ├── subdir1
│ │ └── file1
│ ├── subdir2
│ │ └── file2
│ └── subdir3
│ └── file3
└── rename_spaces.bash
10 directories, 8 files
注:执行脚本“做正确的事”的任何非字母数字似乎是不平凡的。例如,我不确定如何处理文件或目录名中的点或预先存在的下划线或其他“常规”允许的字符。
以通用方式识别不需要/特殊字符也是一个问题。在国际语言环境中它更加复杂。我不知道任何简单的方式说“允许英语字母表中的数字或字符”。如果有人有想法,请继续并发布答案。
您好,这对我来说似乎是完美的解决方案。问你是否也可以用_代替任何非字母数字的东西太多了?我有一些口音,文件夹和文件中的符号。 再次感谢。 – thevoipman 2012-07-24 14:54:39
@thevoipman看看我的编辑。我向脚本添加了对任意特殊字符的支持,并添加了关于如何在文本中配置它的注释。玩的开心。 :) – suvayu 2012-07-24 18:27:57
我重读你的评论,并试图改变脚本做正确的事情,非字母数字。但它似乎是不平凡的。在答案结尾阅读我的笔记。 – suvayu 2012-07-24 18:51:10