Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 6 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .