UNIX 찾기 키워드와 일치하는 여러 파일 이름 바꾸기

UNIX 찾기 키워드와 일치하는 여러 파일 이름 바꾸기

yyy이름에 포함된 파일을 찾기 위한 find 명령의 출력은 다음과 같습니다 .

./plonetheme/xxx/yyy-logo.png
./plonetheme/xxx/profiles/default/plonetheme.yyy_various.txt
./plonetheme/xxx/skins/plonetheme_yyy_custom_images
./plonetheme/xxx/skins/plonetheme_yyy_custom_images/CONTENT.txt
./plonetheme/xxx/skins/plonetheme_yyy_custom_templates
./plonetheme/xxx/skins/plonetheme_yyy_custom_templates/CONTENT.txt
./plonetheme/xxx/skins/plonetheme_yyy_custom_templates/main_template.pt
./plonetheme/xxx/skins/plonetheme_yyy_styles
./plonetheme/xxx/skins/plonetheme_yyy_styles/base_properties.props
./plonetheme/xxx/skins/plonetheme_yyy_styles/CONTENT.txt

yyy문자열 이 로 대체 되도록 모든 파일의 이름을 어떻게 바꾸나요 zzz?

답변1

당신이 사용할 수있는bash 문자열 조작원하는 것을 달성하려면 :

find PATH/PATTERN -exec bash -c 'mv "$0" "${0/yyy/zzz}"' {} \;

스위치 -exec는 escaped까지 명령을 실행합니다 ;. 여기서는 {}현재 처리 중인 파일의 경로입니다.

bash -c 'mv "$0" "${0/cix/stix}"' {}해당 경로를 bash에 인수로 전달합니다. bash는 $0(첫 번째 인수, 예를 들어 ) 를 (첫 번째 조작된 인수, 예를 들어 ) ./plonetheme/xxx/yyy-logo.png로 이동합니다 .${0/yyy/zzz}./plonetheme/xxx/zzz-logo.png

답변2

설명한대로여기-print0, 다음을 사용할 수 있습니다 read -r -d''.

find /path/to/files -type f -print0 | while IFS= read -r -d '' file; do
    mv "$file" "${file/yyy/zzz}"
done

관련 정보