emurad
emurad

Reputation: 3578

find using multiple name patterns

I have this working fine for me:

find Sources/$1-$2 -name '*' |xargs perl -pi -e "s/domain.com/$2/g"

But when I change it to the following it doesn't:

find Sources/$1-$2 -name '*.php,*.rb' |xargs perl -pi -e "s/domain.com/$2/g"

What wrong?

Upvotes: 1

Views: 761

Answers (5)

clt60
clt60

Reputation: 63972

It is much better filtering out find's result with [ef]grep. Why?

Because you can fed the grep pattern as an argument, or can read it from the config or soo. It is much easier to write: grep "$PATTERN" as constructing long find arguments with '-o'. (ofc, here are situations, where find args are better), but not in your case.

The cost is one more process. So, for you example is easy to write a script myscript.sh

find Sources/$1-$2 -print | egrep -i "$3" | xargs ...

you can call it

./myscript.sh aaa bbb ".(php|rb)$"

and the result will equivalent to more complicated

find Sources/$1-$2 \( -iname '*.php' -o -iname '*.rb' \) | xargs ...

but

why bother? If you have bash4+, (and shopt -s globstar in your .bashrc) you can simple write:

perl -pi -e '.....' Sources/aaa-bbb/**/*.{rb,php}

the ** is like a find -name.

Upvotes: 2

ikegami
ikegami

Reputation: 386501

By the way, xargs is not needed here.

find Sources/$1-$2 \( -name '*.php' -o -name '*.rb' \) \
   -exec perl -i -pe "s/domain\.com/$2/g" {} +

Also notice the "." in /domain.com/ needs to be escaped.

Upvotes: 1

FMc
FMc

Reputation: 42421

Here's some explanation behind the solution that others have provided.

The tests in a find command are combined with Boolean operators:

-a  -and
-o  -or
 !  -not

If you don't supply an operator, -and is the default.

find . -type f    -name '*.rb'  # The command as entered.

find . -type f -a -name '*.rb'  # Behind the scenes.

Your search failed because it didn't find any matching files:

# Would find only files with bizarre names like 'foo.php,bar.rb'
find . -name '*.php,*.rb'

You need to supply the file extensions as separate -name tests, combined in an OR fashion.

find . -name '*.php' -o -name '*.rb'

Upvotes: 3

bbaja42
bbaja42

Reputation: 2169

I'm guessing that you want all files then end in .php and .rb.

Try find Sources/$1-$2 \( -iname "*.php" -o -iname "*.rb" \) -print |xargs perl -pi -e "s/domain.com/$2/g"

Upvotes: 2

Op De Cirkel
Op De Cirkel

Reputation: 29493

you have to write it as:

find Sources/$1-$2 -name '*.php' -o -name '*.rb' ....

Upvotes: 2

Related Questions