优化
This commit is contained in:
Executable
+63
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
# 将 CSV 文件按首字符归档到子目录
|
||||
# 用法: bash reorganize_csv.sh /data/project/autoeq/measurements/Eafonyoung/data/in-ear
|
||||
# 支持预览模式: bash reorganize_csv.sh --dry-run /data/project/autoeq/measurements/Eafonyoung/data/in-ear
|
||||
|
||||
DRY_RUN=false
|
||||
if [ "$1" = "--dry-run" ]; then
|
||||
DRY_RUN=true
|
||||
shift
|
||||
fi
|
||||
|
||||
DIR="$1"
|
||||
|
||||
if [ -z "$DIR" ] || [ ! -d "$DIR" ]; then
|
||||
echo "用法: $0 [--dry-run] <目录>"
|
||||
echo "示例: $0 --dry-run /data/project/autoeq/measurements/Eafonyoung/data/in-ear"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
count=0
|
||||
skipped=0
|
||||
|
||||
# 强制使用 C locale 确保字符范围正确
|
||||
export LC_ALL=C
|
||||
|
||||
for csv in "$DIR"/*.csv; do
|
||||
[ -f "$csv" ] || continue
|
||||
|
||||
filename=$(basename "$csv")
|
||||
first_char="${filename:0:1}"
|
||||
|
||||
# 小写字母转大写(使用 tr 确保兼容性)
|
||||
if echo "$first_char" | grep -q '[a-z]'; then
|
||||
subdir=$(echo "$first_char" | tr 'a-z' 'A-Z')
|
||||
else
|
||||
subdir="$first_char"
|
||||
fi
|
||||
|
||||
target_dir="$DIR/$subdir"
|
||||
target_path="$target_dir/$filename"
|
||||
|
||||
# 已经在正确位置的跳过
|
||||
if [ -d "$target_dir" ] && [ "$csv" -ef "$target_path" ]; then
|
||||
skipped=$((skipped + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[预览] $filename -> $subdir/$filename"
|
||||
else
|
||||
mkdir -p "$target_dir"
|
||||
mv "$csv" "$target_path"
|
||||
echo "[移动] $filename -> $subdir/$filename"
|
||||
fi
|
||||
|
||||
count=$((count + 1))
|
||||
done
|
||||
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "--- 预览完成,共 $count 个文件待移动,$skipped 个已跳过 ---"
|
||||
else
|
||||
echo "--- 完成,共移动 $count 个文件,$skipped 个已跳过 ---"
|
||||
fi
|
||||
Reference in New Issue
Block a user