Is it possible to add an option -prune, to not explore sub-directories, when the current directory has been matched ? (like find -prune)
Thank you for your feedback. Can you please give us some examples of actual (non-hypothetical) use cases for this?
@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.
Ok, let's try to implement this as a new flag. We can hide it from the short -h help text.
Hello. I'd like to take this if its available?
Clarification on behavior: is prune meant to be a flag to operate on
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
Yes :) it should work like this (the tool should stop to explore sub-directories when the current directories has matched)
@std-odoo Maybe you could help out with the design decisions in #546?
I'm happy to see that this is in the works!
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.
:slightly_smiling_face:
Implemented in #658 by @reima
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.
I think this use case is covered by -E / --exclude.
@reima Ah, yes, my mistake. Thanks!
released in v8.2.0.
Most helpful comment
I have another use-case to share.
I tried to delete all the
node_modulesfolders on my machine withfd, but found that due to needing a prune-like functionality I could not do this well, and so I had to fallback to usingfind.For those unaware, a
node_modulesfolder is a tree of packages, each with their ownnode_modulesfolders underneath. I wanted to delete all thenode_modulesfolders in my home directory to save space, andfdwas 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
fdfor something like this, rather than a full-blown interactive UI.:slightly_smiling_face: