Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep for a file extension

Tags:

regex

grep

bash

I am currently trying to a make a script that would grep input to see if something is of a certain file type (zip for instance), although the text before the file type could be anything, so for instance

something.zip this.zip that.zip 

would all fall under the category. I am trying to grep for these using a wildcard, and so far I have tried this

grep ".*.zip" 

But whenever I do that, it will find the .zip files just fine, but it will still display output if there are additional characters after the .zip so for instance .zippppppp or .zipdsjdskjc would still be picked up by grep. Having said that, what should I do to prevent grep from displaying matches that have additional characters after the .zip?

like image 511
lacrosse1991 Avatar asked Nov 11 '12 21:11

lacrosse1991


People also ask

How do I grep for a specific file extension?

Specifying File Type To do so, we can use a special type of option that starts with two hyphens: $ grep -ri "hello" --include=*. cc Test/test.cc:Hello World!

How do I search for all files with specific extensions?

For finding a specific file type, simply use the 'type:' command, followed by the file extension. For example, you can find . docx files by searching 'type: . docx'.


1 Answers

Test for the end of the line with $ and escape the second . with a backslash so it only matches a period and not any character.

grep ".*\.zip$" 

However ls *.zip is a more natural way to do this if you want to list all the .zip files in the current directory or find . -name "*.zip" for all .zip files in the sub-directories starting from (and including) the current directory.

like image 97
Chris Seymour Avatar answered Sep 19 '22 23:09

Chris Seymour