Cibin
Cibin

Reputation: 600

How to use multiple filepaths with spaces in find and grep

In Bash, From a file I am reading the list of files matching a pattern to search in. variable content will be something like this after reading

files="C:/Downloads/tutorial java*.txt D:/text materials*.java"

It is then used in find

find  $files -type f -exec grep -PrnIi my-search-term --color=auto {} /dev/null \;

I tried escaping space with '\' like this

files="C:/Downloads/tutorial\ java*.txt D:/text\ materials*.java". 

But not working. I cannot hard code the list of files as it needs to be read from a different file

Upvotes: 0

Views: 71

Answers (1)

chepner
chepner

Reputation: 531075

You are combining paths with the patterns to match in those paths. That's fine, but you would need would need to search basically the entire file system for files matching the full paths.

 find / \( -path "C:/Downloads/tutorial java*.txt" -o -path "D:/text materials*.java" \) ...

If you want to store these in a variable, use an array, not a regular variable.

files=( "C:/Downloads/tutorial java*.txt" "D:/text materials*.java")
patterns=(-path "${files[0]}")
for pattern in "${files[@]:1}"; do
  patterns+=(-o -path "$pattern")
done
find / \( "${patterns[@]"} \)

Upvotes: 1

Related Questions