Fd: `-prune` options

Created on 13 Feb 2020  ยท  14Comments  ยท  Source: sharkdp/fd

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

feature-request good first issue

Most helpful comment

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:

All 14 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sharkdp picture sharkdp  ยท  3Comments

nishithkhanna picture nishithkhanna  ยท  4Comments

runiq picture runiq  ยท  3Comments

mrzool picture mrzool  ยท  4Comments

mwgkgk picture mwgkgk  ยท  3Comments