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

79 views Asked by At

I have this code so far:

#!/bin/bash
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.

1

There are 1 answers

0
choroba On BEST ANSWER

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 [.]).