0

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.

Omar Dulaimi
  • 846
  • 10
  • 30
  • Please add your desired output of `echo "$list"` to your question. – Cyrus Jan 04 '23 at 20:17
  • 3
    `-name 'part.*'`? – Cyrus Jan 04 '23 at 20:24
  • 2
    Regarding looping over the files, don't use `list=$(find ...)` as you'll have problems with spaces and glob characters. Make an array with the result of `find`, by using `-print0` (GNU `find`) and looping over the results with `IFS= read -r -d ''` or using `mapfile`/`readarray`, or if you don't have GNU `find`, use another trick to handle globs and spaces. Depending on what you want to do with your files, you may also consider using the `-exec` action. – gniourf_gniourf Jan 05 '23 at 08:08
  • Thanks for the information, I appreciate it. Still new to the whole bash scripting world. – Omar Dulaimi Jan 05 '23 at 08:36
  • 1
    In isolation, that's a [useless `echo`](https://www.iki.fi/era/unix/award.html#echo), paired with [broken quoting](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable). Probably try https://shellcheck.net/ – tripleee Jan 05 '23 at 09:57

1 Answers1

2

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

choroba
  • 231,213
  • 25
  • 204
  • 289