Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash: find and grep with regex in shellscript

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading