-1

I'm using a shell script on Linux that is processing some files from a directory based on a pattern. The pattern can contain spaces. The question is how do I get the list of files that match the pattern?

Example:
This is the list of files:
file_without_spaces.vol-1.txt
file_without_spaces.vol-2.txt
file with spaces.vol-1.txt
file with spaces.vol-2.txt
file with spaces.vol-3.txt

Result when the pattern is "file_without_spaces":
file_without_spaces.vol-1.txt
file_without_spaces.vol-2.txt

Result when the pattern is "file with spaces":
file with spaces.vol-1.txt
file with spaces.vol-2.txt
file with spaces.vol-3.txt

The pattern comes in an env variable, let's call it PATTERN.

Grepping for the pattern does not work, as it may contain spaces which grep cannot handle. Same for using the pattern as a parameter to find, e.g. find <dir> -name $PATTERN

1 Answers1

0

Grep and find can handle spaces just fine. You simply need to quote them:

find <dir> -name "$PATTERN"
Verpous
  • 588
  • 3
  • 7