4

Linux: I want to list all the files in a directory and within its subdirectories, except some strings. For that, I've been using a combination of find/grep/shell globbing. For instance, I want to list all files except those in the directories

./bin
./lib
./resources

I understand this can be done as shown in this question and this other. But both versions are not solving the case "everything, but this pattern" in general terms.

It seems that it is much easier to use a conditional for filtering the results, but I wonder if there is any compact and elegant way of describing this in regexp or in the shell extended globbing.

Thanks.

Community
  • 1
  • 1
alvatar
  • 3,340
  • 6
  • 38
  • 50

3 Answers3

7

yourcommand | egrep -v "pattern1|pattern2|pattern3"

flybywire
  • 261,858
  • 191
  • 397
  • 503
6

Use prune option of find.

find . -path './bin' -prune -o -path './lib' -prune -o -path './resources' -prune -o «rest of your find params» 
vartec
  • 131,205
  • 36
  • 218
  • 244
3

With bash's extglob shopt setting enabled, you can exclude files with ! in your wildcard pattern. Some examples:

  • Everything except bin, lib, and resources

    shopt -s extglob
    ls -Rl !(bin|lib|resources)
    
  • Everything with an i in it, except bin and lib

    ls -Rl !(bin|lib|!(*i*))
    

    (Everything that has an i in it is the same as everything except the things that don't have i's in them.)

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • The most compact. I've tried similar things, but never realized that if you put the * then anything is matched, so the way was without anything else. I wonder how this could be added to some more restrictions. Like: "everything that has an "i" except "bin" and "lib"... – alvatar Mar 22 '09 at 19:18