Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep command with OR logic

Tags:

grep

bash

shell

sh

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?

like image 522
Gerorge Timber Avatar asked Dec 06 '25 07:12

Gerorge Timber


1 Answers

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.

like image 160
fedorqui 'SO stop harming' Avatar answered Dec 07 '25 20:12

fedorqui 'SO stop harming'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!