grep "*.JPG" no devuelve entradas que terminan en .JPG

grep "*.JPG" no devuelve entradas que terminan en .JPG

Cuando ejecuto este comando ls -l Documents/phone_photo_vids, obtengo cientos de entradas en el siguiente formato. Observe que los finales de las imágenes son PNG o 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

Luego decidí que solo quería ver los resultados en formato jpg, así que ejecuté ambos comandos, pero ninguno de los dos arrojó resultados.

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

Habría esperado que ambos comandos grep filtraran todos los archivos que terminan en PNG y devolvieran todos los archivos que terminan en JPG, pero no obtuve nada. ¿Cómo estoy usando grep incorrectamente?

Estoy trabajando en un Mac OSX 10.9.3

Respuesta1

Algunas formas de respuestas sonEQUIVOCADOaunque funciona como dice la mayor parte del tiempo.

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)

Entonces, para obtener el mejor resultado, prueba estos:

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

Respuesta2

Como han dicho otros, está intentando utilizar comodines de shell ( *) dentro de grep, donde el comodín para un solo carácter es el punto ( .). Patrones de .JPGcoincidirían xxx.NOTAJPGo NOTAJPG.txtsi existiera tal cosa.

La mejor solución es simplemente decir:

ls -l Documents/phone_photo_vids/*.jpg

Si quieres distinguir entre mayúsculas y minúsculas

ls Documents/phone_photo_vids/*.{jpg,JPG}

que es lo mismo que decir ls*.jpg *.JPG

No es recomendable, pero sien realidadSi desea que funcione con grep, simplemente especifique los archivos que terminan en jpgy tal vez haga que no distinga entre mayúsculas y minúsculas con -i. No necesitas todas las '.*.'cosas.

ls -l Documents/phone_photo_vids | grep -i jpg$

Respuesta3

Grep usa lo que se llama expresiones regulares, no lo que usan DOS o Windows para las búsquedas.

La expresión regular "*.JPG$" no tiene sentido para grep, por lo que probablemente la ignore. Lo que quieres es ".*JPG$"

Parareferencia.

Respuesta4

Pruebe lo siguiente:

grep "jpg"    #match string "jpg" anywhere in the filename, so file "img.jpg.txt" match too
grep ".*jpg"  #match the whole line with string "jpg", here ".*" stands for any char zero or more times
grep "jpg$"   #match string "jpg" only at the end of line ("img.jpg.txt" will not match)
grep ".*jpg$" #match the whole line only if "jpg" is at the end of line
grep "\.jpg"  #match string ".jpg" - to search literaly for dot one need to escape it with backslash

Puede crear archivos temporales touch "img.jpg.txt" ".jpg"y usarlos grep --color=alwayspara ver cómo los patrones anteriores cambian la salida.

Por cierto, el análisis lsno suele ser una buena idea, es mejor utilizarlo find:

find /path/to/files/ -maxdepth 1 -type f -iname '*.jpg'

información relacionada