하위폴더 탐색 및 unique_id Renaming

HeungJun Kim·2024년 5월 7일
0

Script

목록 보기
2/5
  • unique_renaming.sh
#!/bin/bash

# 전체 파일 수 계산
total_files=$(find . -type f | wc -l)
processed_files=0

# 함수를 사용하여 무작위 문자열 생성
generate_random_string() {
    # 무작위 문자열 생성
    random_string=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w "$1" | head -n 1)
    echo "$random_string"
}

# 각 폴더에 대해 반복
for folder in */; do
    # 폴더 내의 파일들에 대해 반복
    for file in "$folder"*.*; do
        # 파일이 존재하지 않으면 스킵
        [ -e "$file" ] || continue

        # 파일명 추출
        file_name=$(basename "$file")

        # 유니크한 ID 생성
        unique_id=$(date +%s%N)
        # 무작위 문자열 생성
        random_string=$(generate_random_string 5)
        # 알파벳과 숫자 조합하여 유니크 ID 생성
        unique_id="${unique_id}_${random_string}"

        # 파일의 확장자 추출
        extension="${file_name##*.}"

        # 새로운 파일명 생성
        new_file_name="${unique_id}.${extension}"

        # 파일 이동
        mv "$file" "$new_file_name"

        # 진행 상황 업데이트
        ((processed_files++))
        progress=$((processed_files * 100 / total_files))
        echo "진행률: $progress% ($processed_files/$total_files)"
    done
done

echo "작업이 완료되었습니다."
profile
Computer Vision / ADAS / DMS / Face Recognition

0개의 댓글