Renaming multiple files which match UNIX find keyword
I have the following output from find command for finding files containing yyy
in name:
./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
How I would rename all file so that string yyy
is replaced with zzz
?
You can use bash string manipulation to achieve what you want:
find PATH/PATTERN -exec bash -c 'mv "$0" "${0/yyy/zzz}"' {} \;
The -exec
switch executes the command up to the escaped ;
, where {}
is the path of ther file that's currently handled.
bash -c 'mv "$0" "${0/cix/stix}"' {}
passes that path as an argument to bash, which moves $0
(the first argument, e.g., ./plonetheme/xxx/yyy-logo.png
) to ${0/yyy/zzz}
(the first manipulated argument, e.g., ./plonetheme/xxx/zzz-logo.png
).
As described here, you can use -print0
and read -r -d''
:
find /path/to/files -type f -print0 | while IFS= read -r -d '' file; do
mv "$file" "${file/yyy/zzz}"
done