List files starting with two different prefixes - linux

3643 views regex

I have files:

list-a1.jpg, list-a2.jpg

list-b1.jpg, list-b2.jpg


map-b1.jpg, map-b2.jpg

I want to list them using ls. I want to use regex but I have problem with prefixes. How to specify that my filename should start with "list-" or "map-"? I tried to do:

ls [.map-.][.list-.][a-b][1-2].jpg

but it is not working as expected.

answered question

you could use grep ls | grep '^(map|list)-[a-b][1-2].jpg'

3 Answers


ls accepts multiple file parameters: ls [OPTION]... [FILE]...:

ls list-* map-*

For more control, you could take advantage of bash's curly brace expansion:

ls {list,map}-{a,b}{1,2}.*

posted this

You might use egrep with an alternation and the file extension:

ls | egrep '^(list|map)-[ab][12]\.jpg$

posted this

You could use "extended globbing" - documentation:

shopt -s extglob
ls @(map|list)*

posted this

Have an answer?


Please login first before posting an answer.