Gerorge Timber Gerorge Timber - 3 months ago 8
Bash Question

Grep command with OR logic

I have a directory in which I have numerous files with names like work1, work2, work3, work4 ... workn (where n is any number). I want to only list the files which has

work
as the prefix after that only 1, 2, 3, 4 or 5.

For example: It should list down
work1
work2
work3
work4
work5
. So my logic was
ls -al | grep work
but it will list
work6
,
work7
etc too. How do proceed?

Answer

There is no need to parse the output of ls. Just use a range expression when calling ls:

ls -al work[1-5]

This will match any file whose name matches the pattern work[1-5]. That is, any file whose name is work1, ..., work5.