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

Add --min-depth option #404

Closed
sudo-nice opened this issue Feb 7, 2019 · 10 comments · Fixed by #569
Closed

Add --min-depth option #404

sudo-nice opened this issue Feb 7, 2019 · 10 comments · Fixed by #569

Comments

@sudo-nice
Copy link

We have --max-depth option, but there is no --min-depth counterpart. It could be used exactly like it's been used with find.

@sharkdp
Copy link
Owner

sharkdp commented Feb 7, 2019

Thank you for the feedback.

Please see my comment in #390.

@sudo-nice
Copy link
Author

Thank you for fast reply.
My usecase is pretty simple, I use fd to generate input for shell functions, which help to apply some action (edit, cd, select) to the target. I found it handy to have two modes of such functions, the first one applies only to files (directories) in the current directory, and the second one applies to all the files (directories) under the current PWD. I have easily implemented the first mode, supplying --max-depth 1 to fd. But with the second mode there is an inconvenience, because I'd like to exclude the files (directories) of PWD from the result output.

That's where --min-depth 1 would do exactly what I need, list all the items under the PWD, but excluding the immediate children.
As you can see, for me it would suffice to have something like --min-depth-1 only, but I think a more generic option would be better.

@sharkdp
Copy link
Owner

sharkdp commented Feb 7, 2019

find -mindepth 1 only excludes entries with depth 0, i.e. the current directory (.). fd does this by default. I guess what you really want is -mindepth 2.

-mindepth 2 can be simulated in fd by supplying all directories in the current directory as search paths:

fd … */

Similarly, -mindepth 3 can be simulated via:

fd … */*/

Agreed, it's not as pretty as -mindepth, but that should work (if the argument list is not exceedingly large).

@sudo-nice
Copy link
Author

*/

I didn't think about that. It will take some additional checks and handlers, but I'll figure it out, I guess.
Thank you.

@parkr
Copy link

parkr commented Apr 14, 2020

Hey! I have a case where I want to do --min-depth 6 --max-depth 6, i.e. only list directories at a certain depth. If I do /base/path/*/*/*/*, I get this error:

$ fd-v7.5.0-x86_64-unknown-linux-gnu/fd . /base/path/*/*/*/* --type d --max-depth 6 --change-older-than 1h
-bash: fd-v7.5.0-x86_64-unknown-linux-gnu/fd: Argument list too long

To try to get around bash glob expansion, I used single quotes, but get another error:

$ fd-v7.5.0-x86_64-unknown-linux-gnu/fd . '/base/path/*/*/*/*' --type d --max-depth 6 --change-older-than 1h
[fd error]: '/base/path/*/*/*/*' is not a directory.

Unless I'm missing something, this might be a good motivation for including the --min-depth flag in fd. Sometimes the argument list is too long!

@sharkdp
Copy link
Owner

sharkdp commented Apr 15, 2020

I was about to write that you could use fds new -g/--glob option in combination with -p/--full-path (match the pattern on the full path, not just the basename):

fd -pg '/base/path/*/*/*/*'

but that doesn't actually work, because * currently matches directory separators as well. So you would --max-depth 6 in addition.

I think behavior is actually surprising and we should probably change this. We still have ** to match to arbitrary depths. This fix would be easy (https://docs.rs/globset/0.4.5/globset/struct.GlobBuilder.html#method.literal_separator).

@sharkdp sharkdp reopened this Apr 15, 2020
@sharkdp
Copy link
Owner

sharkdp commented Apr 15, 2020

That said, I kind of agree that --min-depth would be a natural addition. The glob-workaround is neither pretty nor will users likely come up with this. So let's add --min-depth <depth> and --exact-depth <depth>. They could be hidden from the short -h help text.

@sharkdp sharkdp added this to the v8.0 milestone Apr 15, 2020
sharkdp added a commit that referenced this issue Apr 15, 2020
Add new `--min-depth <depth>` and `--exact-depth <depth>` options in addition to the existing
option to limit the maximum depth.

closes #404
@parkr
Copy link

parkr commented Apr 15, 2020

@sharkdp Thank you for thinking about my use case and considering incorporating it into your project. I agree the glob workaround isn't very intuitive, so the --min-depth helps there as well. Your addition of --exact-depth as an alias for --min-depth <depth> --max-depth <depth> further makes my use case more readable and intuitive! I'm excited to give this a spin.

sharkdp added a commit that referenced this issue Apr 15, 2020
Add new `--min-depth <depth>` and `--exact-depth <depth>` options in addition to the existing
option to limit the maximum depth.

closes #404
@sharkdp
Copy link
Owner

sharkdp commented Apr 15, 2020

Thank you for the feedback!

Closed via #568 and #569.

@sharkdp
Copy link
Owner

sharkdp commented Apr 16, 2020

This has now been released in fd v8.0.

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.

3 participants