78

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?

2
  • 1
    this probably shouldn't be labeled regex, * is a shell-globbing wildcard, and is not a regular expression *, which is the "zero or more quantifier". Dec 15, 2011 at 19:57
  • Good point, although I'm learning that using regex is probably better practice for getting the matches you expect.
    – calebds
    Dec 15, 2011 at 20:04

11 Answers 11

95

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.

6
  • 1
    Yay for mentioning the shopt -s extglob! Dec 13, 2012 at 15:23
  • 3
    This is the correct answer. The answer marked as correct doesn't use the shell directly, it uses an ancillary command.
    – dbn
    Jul 8, 2013 at 23:34
  • 1
    @dbw - Even if I like more this answer, the accepted answer is ok for the question. The OP does not specifically ask for using "the shell directly". Oct 13, 2017 at 8:48
  • "egrep-style" is quite misleading, as grep deals with regular expressions, while this here are shell patterns. Jan 31, 2018 at 6:16
  • It did not work for me. For long lists returning about 2000 items, I got "argument list to long". I guess it happens because the branches expand the outputs of the command inside them. So it becomes an argument list with about 2000 items which is really long. Mar 9, 2019 at 8:23
67
ls | grep -v '\.jar$'

for instance.

7
  • How can you do two filetypes?
    – Dan
    Nov 29, 2012 at 1:12
  • 2
    @Silver89, ls | grep -v -e '\.one$' -e '\.two$' for instance. Nov 29, 2012 at 9:36
  • 8
    Don't parse the output of ls, thank you. May 5, 2014 at 20:11
  • 6
    It would be a bit more efficient to use ls --ignore='\.jar' to prevent having to call another program.
    – xizdaqrian
    Jul 3, 2016 at 22:18
  • 1
    ls -1 doesn't do much good, I think it actually happens automatically if output is not a terminal. Problem is that "considered harmful" articles considered harmful :) One has to be aware of all implications when one parses ls output, but if one knows what one's doing one can do whatever one wants :) Sep 29, 2021 at 21:59
42

Little known bash expansion rule:

ls !(*.jar)
2
  • 3
    bash: !: event not found. What do I need?
    – calebds
    Dec 15, 2011 at 20:15
  • 10
    Try executing 'shopt -s extglob' on the command line first, then typing the command. Dec 13, 2012 at 15:22
25

With an appropriate version of find, you could do something like this, but it's a little overkill:

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

find finds files. The . argument specifies you want to start searching from ., i.e. the current directory. -maxdepth 1 tells it you only want to search one level deep, i.e. the current directory. ! -name '*.jar' looks for all files that don't match the regex *.jar.

Like I said, it's a little overkill for this application, but if you remove the -maxdepth 1, you can then recursively search for all non-jar files or what have you easily.

1
  • I do like this one because it works in zsh too, where some of the bash-extension ones do not (at least not with my zsh skill level :D )
    – Greg Lyon
    Oct 25, 2021 at 16:17
18

POSIX defines non-matching bracket expressions, so we can let the shell expand the file names for us.

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

This has some quirks though, but at least it is compatible with about any unix shell.

2
  • +1 for POSIX compliance. A little bash/python/ruby function combined with alias,i.e. "lsnot" could make it easily workable.
    – Nikkolasg
    May 12, 2015 at 7:47
  • 2
    This doesn't match things shorter than three characters. Jan 31, 2018 at 6:18
6

If your ls supports it (man ls) use the --hide=<PATTERN> option. In your case:

$> ls --hide=*.jar

No need to parse the output of ls (because it's very bad) and it scales to not showing multiple types of files. At some point I needed to see what non-source, non-object, non-libtool generated files were in a (cluttered) directory:

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

Worked like a charm.

6

Another approach can be using ls -I flag (Ignore-pattern).

ls -I '*.jar'
1
  • This is so simple and works for mine, many of the others didn't want to work.
    – CTS_AE
    Feb 9 at 7:36
4

And if you want to exclude more than one file extension, separate them with a pipe |, like ls test/!(*.jar|*.bar). Let's try it:

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

Looking at the other answers you might need to shopt -s extglob first.

1
  • 3
    You could shorten this to !(*.[bj]ar) instead. Jan 31, 2018 at 6:19
3

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

3
  • 1
    Don't parse the output of ls, thank you. May 5, 2014 at 20:11
  • 6
    @gniourf_gniourf don't be religious about rules such as parsing ls or using goto. Sometimes those solutions work best. Jul 23, 2014 at 20:39
  • 1
    This isn’t one of those cases; find . -print0 | grep -z '\.jar$' works better even if you don’t want to use --ignore or Bash’s extended globbing. That said, most people don’t want to read a whole article to understand a rule or what they should do, and you can fit a simple explanation and recommendation like “it doesn’t work in edge cases; use find -print0 and grep -z instead” in a comment.
    – Daniel H
    Jan 6, 2017 at 22:25
1

Some mentioned variants of this form:

ls -d *.[!j][!a][!r]

But this seems to be only working on bash, while this seems to work on both bash and zsh:

ls -d *.[^j][^a][^r]
1
ls -I "*.jar"

-I, --ignore=PATTERN
do not list implied entries matching shell PATTERN

  • It works without having to execute anything before
  • It works also inside watch quotes: watch -d 'ls -I "*.gz"', unlike watch 'ls !(*.jar)' which produces: sh: 1: Syntax error: "(" unexpected

Note: For some reason in Centos requires quoting the pattern after -I while Ubuntu does not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.