Wednesday, 21 November 2018

shell script - 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?



Answer



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


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...