0

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

3
  • Your file MySoftware contains string 1.12.0? Commented Dec 8, 2021 at 20:40
  • 1
    @Cyrus: yes, sorry that wasn't clear. I changed to text Commented Dec 8, 2021 at 20:49
  • Given the directory structure in the question, I fail to understand how the command find . -name MySoftware | grep 1.12.0 gives ~/src/MySoftware/1.12.0/ Commented Dec 8, 2021 at 22:44

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. That is working. This solution is adding the term (standard input): in front of the result. Is there a way to NOT do that? I would like to use it in a variable for further processing
I think the output of find contains some non-ASCII characters... And 'grep' thinks that the input is a "binary" file... Try this: find . -type f -name "MySoftware" 2>/dev/null | grep -aE '1.12.0'
Awesome. Thank you very much!

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.