Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash expression to list files beginning and ending with a pattern

Tags:

bash

shell

In my shell bash, I have to select files beginning by ab or xyz and don't end by .jpg or .gif

here is what i did but it doesn't work:

$ echo ab*[!.jpg] ab*[!.gif] xyz*[!.jpg] xyz*[!.gif]

like image 221
Josh Bagwel Avatar asked Jan 03 '23 14:01

Josh Bagwel


2 Answers

With bash's extended glob syntax:

$ touch {ab,xyz}1234.{jpg,gif,txt,doc}

$ shopt -s extglob    
$ echo @(ab|xyz)!(*@(.jpg|.gif))
ab1234.doc ab1234.txt xyz1234.doc xyz1234.txt

The exclamation point is for negation, and the @ symbol is for or.

References:

  • Using OR patterns in shell wildcards
  • How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?
like image 50
user000001 Avatar answered Jan 05 '23 16:01

user000001


Using grep:

ls | grep -E '^ab|^xyz' | grep -E -v '\.jpg$|\.gif$'

-v is to inverse the match

like image 40
Elvis Plesky Avatar answered Jan 05 '23 16:01

Elvis Plesky