List of files that do not match the pattern?

Here you can list all the files matching the template in bash:

ls *.jar 

How to list the addition of a template? that is, all files that do not match * .jar?

+53
bash wildcard glob ls
Dec 15 '11 at 19:20
source share
9 answers
 ls | grep -v '\.jar$' 

eg.

+51
Dec 15 '11 at 19:22
source share

Use advanced egrep style pattern matching.

 ls !(*.jar) 

This is available starting with bash -2.02-alpha1. First you need to enable with

 shopt -s extglob 

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

+67
04 Sep
source share

A little-known bash extension rule:

 ls !(*.jar) 
+33
Dec 15 '11 at 19:29
source share

POSIX defines inconsistent parenthesis expressions, so we can let the shell expand the file names for us.

 ls *[!j][!a][!r] 

It has some quirks, but at least it is compatible with any unix shell.

+17
Sep 12 '14 at 22:46
source share

With the appropriate version of find you can do something similar, but this is a bit overkill:

 find . -maxdepth 1 ! -name '*.jar' 

find finds files. Argument indicates that you want to start a search from . , that is, the current directory. -maxdepth 1 says that you need to search only one level, i.e. current directory. ! -name '*.jar' ! -name '*.jar' searches for all files that do not match the regular expression *.jar .

As I said, this was a bit overkill for this application, but if you remove -maxdepth 1 , you can recursively look for all files other than jar, or whatever is easy for you.

+15
Dec 15 '11 at 19:22
source share

One solution would be ls -1|grep -v '\.jar$'

+2
Dec 15 '11 at 19:22
source share

And if you want to exclude more than one file extension, split them into the channel | e.g. ls test/!(*.jar|*.bar) . Let's try:

 $ mkdir test $ touch test/1.jar test/1.bar test/1.foo $ ls test/!(*.jar|*.bar) test/1.foo 

Looking for other answers, you might need shopt -s extglob .

+2
Aug 15 '17 at 7:42 on
source share

If your ls supports this ( man ls ), use --hide=<PATTERN> . In your case:

 $> ls --hide=*.jar 

There is no need to analyze the output of ls (because it is very bad), and it scales so as not to show multiple file types. At some point, I needed to see which non-source, non-object, non-generated libtool files were in the (cluttered) directory:

 $> ls src --hide=*.{lo,c,h,o} 

Worked like a charm.

+2
Sep 06 '18 at 12:20
source share

Another approach might be to use the ls -I (Ignore-pattern) flag.

 ls -I '*.jar' 
0
May 28 '19 at 6:59
source share



All Articles