Exclude a subdirectory using find

I have a directory structure like this

data |___ | abc |____incoming def |____incoming |____processed 123 |___incoming 456 |___incoming |___processed 

There is an inbound subfolder in all folders within the data directory. I want to get all files from all folders and subfolders except inbox / inbox and 456 / inbox. I tried the following command

  find /home/feeds/data -type d \( -name 'def/incoming' -o -name '456/incoming' -o -name arkona \) -prune -o -name '*.*' -print 

but it does not work as expected.

Ravi

+65
unix bash
Nov 19 '12 at 19:11
source share
3 answers

It works:

 find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*" 

Explanation:

  • find /home/feeds/data : start the search recursively from the specified path
  • -type f : find only files
  • -not -path "*def/incoming*" : do not include anything with def/incoming as part of the path
  • -not -path "*456/incoming*" : do not include anything with 456/incoming as part of its path
+122
Nov 19 '12 at 19:22
source share

-name matches only the file name, not the whole path. Instead, you want to use -path , for parts in which you trim directories, such as def/incoming .

+4
Nov 19 '12 at 19:16
source share

Just for the sake of documentation: you may have to dig deeper, as there are many search constellations (like me). It may turn out that prune is your friend, and -not -path will not do what you expect.

So, this is a valuable example of 15 search examples that exclude directories:

http://www.theunixschool.com/2012/07/find-command-15-examples-to-exclude.html

To relate the original question, the exception finally worked for me as follows:

 find . -regex-type posix-extended -regex ".*def/incoming.*|.*456/incoming.*" -prune -o -print 

Then, if you want to find one file and still exclude patterns, just add | grep myFile.txt | grep myFile.txt .

This may depend on your version of the search. I see:

 $ find -version GNU find version 4.2.27 Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX 
+4
Aug 04 '14 at 7:15
source share



All Articles