anduin revised this gist . Go to revision
1 file changed, 59 insertions
rename_all.sh(file created)
@@ -0,0 +1,59 @@ | |||
1 | + | #!/usr/bin/env bash | |
2 | + | ||
3 | + | #----------------------------------------------------- | |
4 | + | # 先收集所有“需要重命名”的目录,再统一重命名 | |
5 | + | #----------------------------------------------------- | |
6 | + | rename_dirs_deepest_first() { | |
7 | + | echo "==> Searching for directories containing 'NiBot' ..." | |
8 | + | ||
9 | + | # 收集所有包含“NiBot”的目录,排除 .git | |
10 | + | # -path '*/.git*' -prune -o :跳过 .git | |
11 | + | # -type d :只搜索目录 | |
12 | + | # -depth :让 find 先列出子目录,再列出父目录(自下而上) | |
13 | + | # -print :把结果打印到 stdout | |
14 | + | mapfile -t DIRS < <(find . -path '*/.git*' -prune -o -type d -name "*NiBot*" -depth -print) | |
15 | + | ||
16 | + | # 逐个重命名,越深的目录会在前面列出(因为-depth先处理子目录) | |
17 | + | for old_dir in "${DIRS[@]}"; do | |
18 | + | # 生成新名字 | |
19 | + | new_dir=$(echo "$old_dir" | sed 's/NiBot/Voyager/g') | |
20 | + | ||
21 | + | # 如果名称确实不同就执行 mv | |
22 | + | if [[ "$old_dir" != "$new_dir" ]]; then | |
23 | + | echo "Renaming directory:" | |
24 | + | echo " $old_dir" | |
25 | + | echo "-> $new_dir" | |
26 | + | mv "$old_dir" "$new_dir" | |
27 | + | fi | |
28 | + | done | |
29 | + | } | |
30 | + | ||
31 | + | #----------------------------------------------------- | |
32 | + | # 收集所有“需要重命名”的文件,再统一重命名 | |
33 | + | #----------------------------------------------------- | |
34 | + | rename_files_deepest_first() { | |
35 | + | echo "==> Searching for files containing 'NiBot' ..." | |
36 | + | ||
37 | + | # 收集所有包含“NiBot”的文件,排除 .git | |
38 | + | mapfile -t FILES < <(find . -path '*/.git*' -prune -o -type f -name "*NiBot*" -depth -print) | |
39 | + | ||
40 | + | # 逐个重命名 | |
41 | + | for old_file in "${FILES[@]}"; do | |
42 | + | new_file=$(echo "$old_file" | sed 's/NiBot/Voyager/g') | |
43 | + | ||
44 | + | if [[ "$old_file" != "$new_file" ]]; then | |
45 | + | echo "Renaming file:" | |
46 | + | echo " $old_file" | |
47 | + | echo "-> $new_file" | |
48 | + | mv "$old_file" "$new_file" | |
49 | + | fi | |
50 | + | done | |
51 | + | } | |
52 | + | ||
53 | + | #----------------------------------------------------- | |
54 | + | # 主流程:先处理目录,再处理文件 | |
55 | + | #----------------------------------------------------- | |
56 | + | rename_dirs_deepest_first | |
57 | + | rename_files_deepest_first | |
58 | + | ||
59 | + | echo "All done!" |
Newer
Older