Morse
Morse

Reputation: 1562

wildcard expansion in bash script

if I write this string in my script:

list=$(ls /path/to/some/files/*/*/*.dat)

it works fine. But what I need is

files="files/*/*/*.dat"
list=$(ls /path/to/some/${files})

and it says

ls: /path/to/some/files/*/*/*.dat: No such file or directory

How should I do it?

Upvotes: 6

Views: 7507

Answers (2)

chepner
chepner

Reputation: 531165

If you only get that message where there truly are no matching .dat files, add this to your script:

shopt -s nullglob

It will cause the glob to expand to an empty list if there are no matching files, rather than being treated literally.

Upvotes: 19

kev
kev

Reputation: 161674

Try this:

list=$(find /path/to/some/files/ -mindepth 3 -maxdepth 3 -name '*.dat')

Upvotes: 4

Related Questions