0

So I asked this question on how to find a directory that contains a specific character.

Now I'd like to change that character to something else (not the whole name just that one character)

Example:

find "a"
     aaa 
     bab
replace with "c"
     ccc
     bcb

How do I do that?

This is the code I have atm

read -p "find what character: " findwhat
find . -name "*$findwhat*" -type d -print
phuclv
  • 30,396
  • 15
  • 136
  • 260

1 Answers1

0

One approach would be to use rename (aka prename). To replace _ with -:

rename 's/_/-/g' dirname

(the g replaces all occurrences. Omit it if you only want to replace one occurrence)

Combining this with your find statement:

find . -name "*$findwhat*" -type d -execdir rename "s/$findwhat/$replacewith/g" {} \;

You might need to modify this to get the variable substitution to work correctly.

Note: The {} tells -execdir/-exec where to insert the filename in the command; the ; tells it where the commands ends - and you need to escape it with \ because otherwise the shell will treat it as a command separator.

Joe P
  • 511