Home > Back-end >  bash: find and grep with regex in shellscript
bash: find and grep with regex in shellscript

Time:12-09

I have several versions of a certain software (lets call it MySoftware)installed and I like to find the path to a specific version with a combination of find and grep. Assume I have the following versions: 1.12.0
1.12.2
1.42.2

It is stored in the following way:

~/src/MySoftware/1.12.0/...
~/src/MySoftware/1.12.2/...
~/src/MySoftware/1.42.2/...

In a shell I could do something like find . -name MySoftware | grep 1.12.0. This is working since the command is giving me the ~/src/MySoftware/1.12.0/ path.

However, when switching to a shell script, I try to do this:

find . -name "MySoftware"  -exec grep "1\.12\.0" {} ';'

The example above is, however, not returning anything and I have no idea why. Other tries with grep -HF "1.12.0" are also not working. I am grateful for any advice

CodePudding user response:

Please note the difference!

In the first example, find . -name MySoftware | grep 1.12.0 you are building a list with the names of the files and then you grep the specific version.

However, in the second example you are trying to find same files AND you are trying to grep in those files!!!

So, with this:

find . -name "MySoftware"  -exec grep "1\.12\.0" {} ';'

You are searching in those files!!!

I would suggest you to do this:

find . -type f -name "MySoftware" | grep 1.12.0
  • Related