1

I'd like to find all files that contains the string " Of ", replacing with " of", except for strings with a hyphen before 'Of' : "- Of ".

First I had this command :

find . -depth -name "* Of *" -exec bash -c 'for f; do base=${f##*/}; mv -- "$f" "${f%/*}/${base// Of / of }"; done' _ {} +

But I want to use regex for the "- Of " exclusion.

How can I do that ?

I'm confused with sed regex.

Using

find . -regextype sed -regex '.* Of *'

it only founds path ending with Of.

Patrick

1
  • Please, does someone have a simple solution for this? Commented Nov 12, 2014 at 1:50

1 Answer 1

1

Try doing this with rename :

$ find . -depth -name "* Of *" -exec rename -n 's|(?<!-)\s+Of|of|' {} +

from the shell prompt. It's very useful, you can put some code like I does in a substitution.

You can remove the -n (dry-run mode switch) when your tests become valids.

I use negative look behind advanced REGEX

warning There are other tools with the same name which may or may not be able to do this, so be careful.

If you run the following command (linux)

$ file $(readlink -f $(type -p rename))

and you have a result like

.../rename: Perl script, ASCII text executable

then this seems to be the right tool =)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo update-alternatives --set rename /path/to/rename

(replace /path/to/rename to the path of your perl's rename command.


If you don't have this command, search your package manager to install it or do it manually


Last but not least, this tool was originally written by Larry Wall, the Perl's dad.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, but where exactly do I put rename -n 's|(?<!-)\s+Of|of|' * ?
In a shell where the directory with Of files resides
Sorry again, you lost me completely. Thanks for trying nonetheless.
It is not working. I get this : rename: unknown option -- n . And if I remove the -n : rename: not enough arguments
So you don't have the right rename command, see my explanations in my original POST
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.