4

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]

Inian
  • 80,270
  • 14
  • 142
  • 161

3 Answers3

6

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:

user000001
  • 32,226
  • 12
  • 81
  • 108
3

Using grep:

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

-v is to inverse the match

Murphy
  • 3,827
  • 4
  • 21
  • 35
Elvis Plesky
  • 3,190
  • 1
  • 12
  • 21
1

Hi you can try with below command:-

ls {ab*,xyz*}.* | sed '/.jpg/d;/.gif/d'

IF you store the output into a file:-

ls {ab*,xyz*}.* | sed '/.jpg/d;/.gif/d' > shortedFile.txt

How will it work? ls {ab*,xyz*}.* command will list out all the files begin with ab and xyz and redirect the output to sed command by using | (pipe) and sed command will remove file name ended with .jpg and gif.

Hope this will help your

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17