Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-prune options #535

Closed
std-odoo opened this issue Feb 13, 2020 · 15 comments · Fixed by #658
Closed

-prune options #535

std-odoo opened this issue Feb 13, 2020 · 15 comments · Fixed by #658

Comments

@std-odoo
Copy link

Is it possible to add an option -prune, to not explore sub-directories, when the current directory has been matched ? (like find -prune)

@sharkdp
Copy link
Owner

sharkdp commented Feb 16, 2020

Thank you for your feedback. Can you please give us some examples of actual (non-hypothetical) use cases for this?

@std-odoo
Copy link
Author

std-odoo commented Feb 17, 2020

@sharkdp Yes of course :)
I'm developing a package for sublime text, and I'm using your tool
https://github.com/Mister7F/Sublime-DirectoryFilter

I don't want to display a directory if one of the parent directory has matched the search :)

With the find tool, I can use the option prune, but as your tool is faster, I use it, and I prune the directory manually, in python.

@sharkdp
Copy link
Owner

sharkdp commented Feb 28, 2020

Ok, let's try to implement this as a new flag. We can hide it from the short -h help text.

@neuronull
Copy link

Hello. I'd like to take this if its available?

@neuronull
Copy link

Clarification on behavior: is prune meant to be a flag to operate on <pattern>?
for example:

$ tree
.
├── bar
│   └── zap
│       ├── foo
│       └── zig
└── foo
    ├── bar
    └── foo

$ fd foo
bar/zap/foo
foo
foo/foo

$ fd --prune foo
bar/zap/foo
foo

@std-odoo
Copy link
Author

Yes :) it should work like this (the tool should stop to explore sub-directories when the current directories has matched)

neuronull pushed a commit to neuronull/fd that referenced this issue Mar 13, 2020
- Added --prune option which will not descend into directories
  that are a match on pattern.
- Added test to cover --prune option.
@sharkdp
Copy link
Owner

sharkdp commented Mar 28, 2020

@std-odoo Maybe you could help out with the design decisions in #546?

@jonathan-s
Copy link

I'm happy to see that this is in the works!

@acheronfail
Copy link

acheronfail commented Jul 2, 2020

Thank you for your feedback. Can you please give us some examples of actual (non-hypothetical) use cases for this?

I have another use-case to share.
I tried to delete all the node_modules folders on my machine with fd, but found that due to needing a prune-like functionality I could not do this well, and so I had to fallback to using find.

For those unaware, a node_modules folder is a tree of packages, each with their own node_modules folders underneath. I wanted to delete all the node_modules folders in my home directory to save space, and fd was matching the subpackages as well, which means if I'd run -x rm -rf {} then it would have failed since a higher level directory could have been deleted before a lower level directory.
With prune this would not have been an issue.

Also, this is a common issue, common enough that there's a utility just for this: https://github.com/voidcosmos/npkill. But I would rather use fd for something like this, rather than a full-blown interactive UI.

🙂

@reima reima mentioned this issue Oct 3, 2020
sharkdp pushed a commit that referenced this issue Oct 25, 2020
@sharkdp
Copy link
Owner

sharkdp commented Oct 25, 2020

Implemented in #658 by @reima

@sasquires
Copy link

Unfortunately I only saw this issue today, after it was closed, but I want to point out that the use of -prune in find is much greater than just pruning directories that match the same pattern as the filename. For me, the main usage is automatically excluding directories where I know in advance that I will not find the file I am looking for.

For example, suppose that I am searching files that match a pattern in all of the tools installed on my system, but I do not want documentation files. Moreover, running find or fd blindly, most of the time is spent in documentation subdirectories because they contain a lot of files. In this case I can do

find \( -name doc -o -name docs \) -prune -o -regex pattern -print

This will skip the processing of anything named doc or docs. I recently found in a similar case that I got a factor of 15 in speed by applying a few filters like this.

It may be too late to support this in fd but I hope that it's still possible to re-open this issue. Thanks.

@reima
Copy link
Contributor

reima commented Nov 17, 2020

I think this use case is covered by -E / --exclude.

@sasquires
Copy link

@reima Ah, yes, my mistake. Thanks!

@sharkdp
Copy link
Owner

sharkdp commented Dec 6, 2020

released in v8.2.0.

@ip-rw
Copy link

ip-rw commented Jul 17, 2022

this prune behaviour is perfect when descendants of a match are no longer relevant.

perhaps you want to delete every directory called 'node_modules' :)

ty for adding it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants