Home > Software engineering >  How to find all files that match a regex and store the result in a variable?
How to find all files that match a regex and store the result in a variable?

Time:01-05

I have this code so far:

list=$(find "$(dirname $0)/../temp/" -regex "(part.[a-z])")
echo $list

I want to find all files in the temp directory that have a file name like this:

part.aa part.ab part.aaa part.qqk

current path is:

./scripts/script.sh

path where files are in:

../temp

Ideally, the result stored in list should be in a form that allow looping over it line by line.

CodePudding user response:

The regex must match the whole path.

-regex '.*/part\.[a-z] '

Note that . is special in regexes, it matches any character. To match a literal dot, you need to backslash it (or use [.]).

  • Related