Filtering file-list using grep

Karun :

I am trying to list files in a specific directory whose name do not match a certain pattern. For eg. list all files not ending with abc.yml For this I am using the command:

ls | grep -v "*abc.yml"

However I still see the files ending with abc.yml, what am I doing wrong here?

choroba :

Asterisk has a different meaning in regular expressions. In fact, putting it to the front of the expressions makes it match literally. You can remove it, as grep tries to match the expression anywhere on the line, it doesn't try to match the whole line. To add the "end of line" anchor, add $. Also, . matches any character, use \. to match a dot literally:

ls | grep -v 'abc\.yml$'

In some shells, you can use extended globbing to list the files without the need to pipe to grep. For example, in bash:

shopt -s extglob
ls !(*abc.yml)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350555&siteId=1
Recommended