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
  • 26,555
  • 15
  • 113
  • 235
Devid Demetz
  • 315
  • 1
  • 2
  • 6

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
  • 451
  • 3
  • 9
  • thanks it works. but why do i need that {} and backslash? – Devid Demetz Jun 11 '17 at 15:53
  • 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 Jun 11 '17 at 15:56
  • Added it to the answer :) – Joe P Jun 11 '17 at 17:00