Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files not matching a pattern?

Here's how one might list all files matching a pattern in bash:

ls *.jar 

How to list the complement of a pattern? i.e. all files not matching *.jar?

like image 376
calebds Avatar asked Dec 15 '11 19:12

calebds


People also ask

How do I find files that do not contain a given string pattern?

You can do it with grep alone (without find). grep -riL "foo" . -L, --files-without-match each file processed. -R, -r, --recursive Recursively search subdirectories listed.

What is pattern matching in Linux?

Wildcards allow you to specify succinctly a pattern that matches a set of filenames (for example, *. pdf to get a list of all the PDF files). Wildcards are also often referred to as glob patterns (or when using them, as "globbing").


2 Answers

Use egrep-style extended pattern matching.

ls !(*.jar) 

This is available starting with bash-2.02-alpha1. Must first be enabled with

shopt -s extglob 

As of bash-4.1-alpha there is a config option to enable this by default.

like image 69
Christian Avatar answered Sep 19 '22 13:09

Christian


ls | grep -v '\.jar$' 

for instance.

like image 44
Michael Krelin - hacker Avatar answered Sep 20 '22 13:09

Michael Krelin - hacker