[SOLVED] Can't exclude folder from search using Shell

I'm using this script:

find "$KMVAR_Local__mainDir" -type f -mmin -30 -not -path "/Users/dannywyatt/Library/Saved Application State/*"

but when I run it, I keep getting this:
image

If I'm excluding that folder, why is KM still searching inside?
I tried the same script with a "tests" folder, and it's working, so the issue doesn't seem to be the script itself. It's something related to that folder, but if it's supposed to be excluded, why is it trying to search there?

find will try to traverse all directories even if they are excluded from the results, so if you dont' have permission, the script will still throw an error. I believe you need to prune the folder from the search instead.

find "$KMVAR_Local__mainDir" -path "/Users/dannywyatt/Library/Saved Application State/*" -prune -o -type f -mmin -30

What does "traverse" mean?

Funny enough, my first test was with prune, but I couldn't make it work:

I'm a bit confused... doesn't the -o mean "or"?
If so, can you explain what the script is doing?

I tried your script and it ran without issues, but I see that the directory is still not excluded:
image

I'm noticing that it's listing files that weren't supposed to be there. Take this one as an example:

It isn't modified since April 2023 and yet, it shows up in the results.
Do you know why this is happening and how to make this search accurate?

The -o is OR. According to the find man page, this is how prune is intended to work:

The idea here is that the expression before -prune matches
things which are to be pruned. However, the -prune action
itself returns true, so the following -o ensures that the
right hand side is evaluated only for those directories
which didn't get pruned (the contents of the pruned
directories are not even visited, so their contents are
irrelevant).

I noticed the same when i just tested again, try removing the *

find "$KMVAR_Local__mainDir" -path "/Users/dannywyatt/Library/Saved Application State/" -prune -o -type f -mmin -30

I understand the concept (kinda), but it seems a bit confusing and non-logical, don't you think?

Doing that, goes back to showing the error with the permissions...

Definitely. I think i'm a bit stumped here, I've used -prune in some find commands, but in much smaller directories than a /Library/ folder.

Hopefully someone else can help! I'm afraid i'm out of my area of expertise (and out of time) :saluting_face:

1 Like

No problem, I understand.
If you have time, do you know why I would still get files that were not modified in the last 30min still showing up? That's another weird thing.

It seems that the issue with those files showing up, even though their modification date is not a match, is also related to that same folder "Saved Application State". Since I don't need to have that on my list, I just used the initial script you shared that throw no errors, saved that to a variable, then used the Search and Replace with:
^(\/Users\/dannywyatt\/Library\/Saved Application State\/.*)
to remove those lines.

Sometimes you gotta make it work some other way :wink:

Thanks for sharing that prune solution. It really helped! :raised_hands:

Quick update

After some more tests, I found out that the only way to make it work and exclude that folder is to add -print at the end, like this:

find "$KMVAR_Local__mainDir" -path "/Users/dannywyatt/Library/Saved Application State" -prune -o -type f -mmin -30 -print

I also tried a few more versions to make sure I know the ones that work and those that don't. Here's what I got:

THESE WORK (no permission errors and they exclude the folder):

find "$KMVAR_Local__mainDir" -path /Users/dannywyatt/Library/Saved\ Application\ State -prune -o -type f -mmin -30 -print
find "$KMVAR_Local__mainDir" -path "/Users/dannywyatt/Library/Saved Application State" -prune -o -type f -mmin -30 -print

THESE DON'T WORK (I get the permissions error):

find "$KMVAR_Local__mainDir" -path ./Library/Saved\ Application\ State -prune -o -type f -mmin -30 -print
find "$KMVAR_Local__mainDir" -path ./Library/Saved\ Application\ State/* -prune -o -type f -mmin -30 -print
find /Users/dannywyatt -path ./Library/Saved\ Application\ State -prune -o -type f -mmin -30 -print
find /Users/dannywyatt -path ./Library/Saved\ Application\ State/* -prune -o -type f -mmin -30 -print
1 Like

nice! glad you figured it out! find is so fickle with the syntax