How to find / grep all paths where the directory name matches "x" but not "y" on * nix?

I have a folder structure like this (this is a small snippet):

└── test   └── cases     └── model       β”œβ”€β”€ client       β”‚ β”œβ”€β”€ socketsTest.coffee       β”œβ”€β”€ server       β”‚  └── socketsTest.coffee       └── shared       └── findersTest.coffee 

The question is, how do you list all the paths that end in .coffee and do not exist in the client folder?

The following command returns all files matching .coffee that exist in the server folder:

 find test -name "*Test.coffee" | egrep '/*server*/' 

But I really need a regular expression that matches everything except that in the client folder.

What is the cleanest way to do this on * nix? The final goal is to return the files not inside the client folder, so for the tree above, it will

 $ <find files except those a client folder> test/cases/model/server/socketsTest.coffee test/cases/model/shared/findersTest.coffee 

I tried to do something similar, but no luck:

 find test -name "*Test.coffee" | egrep '*model/[^client]*' 
+6
source share
2 answers

You can use the -prune action to ignore directories. -o means "or," so read it like "if he called click to crop it, otherwise print files named *.coffee ".

 find test -name client -prune -o -name '*.coffee' -print 

Or you can use a double test, which is easier to read, but slightly less efficient, as it will recursively go into the client/ directories, while the first completely avoids them.

 find test -name '*.coffee' ! -wholename '*/client/*' 
+10
source

I would prefer the -prune answer given by John Kugelman.

Another way to solve this problem with grep is to use the -v option:

 find test -name "*Test.coffee" | grep -v client 

-v inverts pattern matching, so all matches containing "client" are discarded

+1
source

Source: https://habr.com/ru/post/924484/


All Articles