Bash expression for a list of files starting and ending with a pattern

In my bash shell, I need to select files starting with abor xyzand not ending with .jpgor.gif

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

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

+4
source share
3 answers

With bash 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

An exclamation mark for negation, and a symbol @for or.

Literature:

+6
source

Using grep:

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

-v - invert match

+2
source

, : -

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

: -

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

How will it work? The command ls {ab*,xyz*}.*will list all files starting with aband xyz, and redirect the output to the command sedusing the command |(pipe) and sed, will delete the file name ending with .jpgand gif.

Hope this helps your

+1
source

Source: https://habr.com/ru/post/1695512/


All Articles