Exclude folders with a path pattern using FIND command on Ubuntu console

0

PROJECTS
   |___ SITE1
   |      |____files (to exclude)
   |      |____img
   |      |____js
   |
   |___ SITE2
   |      |____files (to exclude)
   |      |____img
   |      |____js
   |
   |___ SITE3
          |____files (to exclude)
          |____img
          |____js

Hello, I’d like to search into SITEx folders excluding content of files folders.

I try this but it display me permissions errors for files into files folders (so seem to not exclude them) :

find . ! -path "*/files/*" -type f -name 'foo'

It display (I’m in PROJECTS directory):

./SITE1/img/foo.png
./SITE1/js/foo.js
find: `./SITE1/files/201501': Permission denied
find: `./SITE1/files/201412': Permission denied
find: `./SITE1/files/201501': Permission denied
... (the two firsts matches are pretty good, but I break the execution after, it print too many indesirble matches)

I was inspired by this response on askubuntu.

I also try the -prune option like in this example without finding a solution…


I’d like to have the equivalent of this search with egrep but for filenames

egrep -Rns --exclude-dir=files 'foo' *

Thanks for your help !

onirix

Posted 2015-01-13T17:04:05.863

Reputation: 41

We need way more information. I have no idea what you are asking. You have no direct question. Try writing again so there is a specific question you are asking. – Eric F – 2015-01-13T17:08:33.393

1Unless I'm mistaken, which is possible, an exclusion causes the "hits" to be not displayed, but does not stop the directories from being looked into. The documentation (the --help output, the man page or something other) for the find command should indicate how find behaves. Did you look into this documentation? – killermist – 2015-01-13T17:18:07.113

This is actually disturbingly easy to do in Windows 8... are you stuck with find on Linux or are you able to use Windows for this? – Mokubai – 2015-01-13T18:11:38.370

Try the -prune directive of find. I suggest that you search for examples (there are a few good ones right here at Super User), because the man page by itself is not very illuminating on the subject of how to use -prune. – G-Man Says 'Reinstate Monica' – 2015-01-13T18:44:42.320

@Mokubai It's disturbing how much easier this is on *nix than on Windows 8 – None – 2015-01-17T09:38:25.227

1@BroSlow On Windows 8 all you do is click inside the search box and type -"files" -directory and it does exactly what is asked without having to first open a new terminal window, cd'ing to the directory, then figuring out the syntax of the find command. The *nix seems a lot like the "long" way. But I'm not here to evangelise, I was just wanting to see if other options were available or if another method would help. – Mokubai – 2015-01-17T10:23:45.083

@Mokubai That would be an ok option for this example, but it's still less precise (i.e. what if you have a file called files?). – None – 2015-01-17T18:31:37.097

Answers

0

Ok done !

I didn’t know how to use prune argument. This respond to my demand

find * -path "*/files/*" -prune -o -name 'foo' -type f -print

More explication here


On the other hand I should be curious if there is a syntax with the ! (NOT) argument like in this example on AskUbuntu if someone have an idea.

onirix

Posted 2015-01-13T17:04:05.863

Reputation: 41

Yes, but the AskUbuntu example you linked doesn't stop find from continuing to traverse those directories, so you would still get the errors. Your syntax is not worse in any way, and is at worst never slower (almost always faster). Although, note that by using *, instead of ., you are likely omitting dotfiles in your top level directory – None – 2015-01-17T09:36:14.143