Linux – grep “*.JPG” not returning entries ending in .JPG

greplinux

When I run this command ls -l Documents/phone_photo_vids, I get 100s of entries in the following format. Notice the endings of the images are either PNG or JPG

-rw-r--r--  1 moi  staff      189280 Oct 29  2011 IMG_0041.PNG
-rw-r--r--  1 moi staff     2481306 Oct 29  2011 IMG_0042.JPG

I then decided that I only wanted to see the jpg results, so I ran both of these commands, both of which returned no results

 ls -l Documents/phone_photo_vids | grep "*.JPG"
 ls -l Documents/phone_photo_vids | grep "*.JPG$"

I would have expected both of the grep commands to filter out all the files ending in PNG, and return all the files ending in JPG, but I got nothing. How am I using grep incorrectly?

I'm working on a Mac OSX 10.9.3

Best Answer

Some form of answers are WRONG although it works like it claims most of the time.

grep ".jpg"    #match string "jpg" anywhere in the filename with any character in front of it.
               # jpg -- not match
               # .jpg -- match
               # mjpgsfdfd -- match
grep ".*.jpg"  #basically the same thing as above
grep ".jpg$"   #match anything that have at least 4 chars and end with "jpg"
               # i_am_not_a_.dummy_jpg -- match
grep ".*.jpg$" #the same as above (basically)

So to have the best result, try these ones:

grep "[.]jpg$" #anything that end with ".jpg"
grep "\\.jpg$" #the same as above, use escape sequence instead
Related Question