Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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).

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.