1

I have files like file123.txt, file.txt

but when I run ls -l file[0-9]*.txt, I'm only getting file123.txt.

How do I get both files? I thought * means "0 or more", but it's not working. (I'm using ksh, if relevant).

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
Adriana
  • 41
  • 5

2 Answers2

3

It is Pattern matching, not a regular expression.

[0-9] and * are distinct patterns. * is not a modifier as in a regular expression.

* Matches any string, including the null string.
[...]  Matches  any  one  of the enclosed characters.

You may use a pattern-list where the * is a prefix to the enclosed pattern-list

*(pattern-list) Matches zero or more occurrences of the given patterns.

ls file*([0-9]).txt would be the syntax for zero or more digits.

$ ls file*.txt
file123.txt  filea.txt  file.txt
$ ls file*([0-9]).txt
file123.txt  file.txt

For details see 'File Name Generation' in man ksh. Some more examples can be found here KSH93 Extended Patterns

ULick
  • 969
  • 6
  • 12
0

You're adding a number filter, just remove this to return files that start with 'file' and end with '.txt':

ls -l file*.txt
  • ls -l file[0-9]*.txt - what about this one ? Does it expect atleast one number after file ? I checked two tutorials and they say [0-9]* expects zero or more. So in that case my command should work ? – Adriana Mar 23 '18 at 13:18
  • Yes, [0-9] will expect at least one number. Possibly the tutorials were referring to the number zero? You can test both commands quickly to see. – Gareth O'Connor Mar 23 '18 at 14:37
  • @Adriana Possibly the tutorials described `Regular Expressions`? The set of symbols is similar but the meaning is different from Bash `Pattern Matching` for filenames. – Lars Fischer Mar 23 '18 at 19:07