-
-
Notifications
You must be signed in to change notification settings - Fork 866
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
Negating patterns: add --exclude-regex? #198
Comments
Is there really a strong use case for this? We already have the
Not really. Negative lookaheads can be abused for that but (a) that's not really practical and (b) I don't think they are supported by the regex crate. A workaround would be |
Personally, I needed it to remove all files directory which did not fit a given criteria. For instance, deleting all files not starting with the prefix "keep": fd '^(?!keep)' --exec rm {} Even if fancy regexes aren't supported, an fd 'keep' --invert --exec rm {} |
@SicariusNoctis fd -E 'keep*' -X rm |
@sharkdp Why not add an |
I too would like an |
Ok, reopening for now. |
I just ran into the issue of a lack of exclude patterns myself. I'm working on some Blender addon and I need to exclude just one |
@razcore-rad I'm pretty sure you could use |
I see. That's handy. I looked at the man page but it didn't mention the specific syntax. I did try |
It uses the same syntax as .gitignore |
ripgrep supports this as well within
|
@M1cha |
Using globbing instead of regex for This would be breaking behaviour though, so perhaps that's not acceptable. |
I don't think we could do that. Not only is it a breaking change, but it would break things in a subtle way where some existing usages would work, some wouldn't work at all, and others would work sometimes. Also, fwiw, the current --exclude option is designed to be consistent with entries in an ignore file. |
--exclude-regex (or --invert-match in grep) Needed for My Use CaseI'm writing a script to calculate the total playtime of all videos in a directory recursively using
When I want to list the files with serial numbers from 1 to 4, I use However, when I need an inverted match, |
@aqdasak For your use case you could use $ fd | grep -E '(^|/)[1-4]\)'
$ fd | grep -Ev '(^|/)[1-4]\)' |
The command Currently, I'm using the following method (in fish shell): for i in (fd)
basename $i | grep -i '^[1-4])' >/dev/null && echo $i
end and for i in (fd)
basename $i | grep -iv '^[1-4])' >/dev/null && echo $i
end |
Oh right, then something like |
Using a second program will negate advantages such as coloring. |
For me, this is the problem I run into unfortunately frequently enough for me to find this thread: Piping to another program has 2 disadvantages, as NightMachinery said you lose the coloring, and it also dramatically increases the search time because it has to match everything you want to exclude before you can actually exclude it. I actually came here hoping there was an option to change the behavior of --exclude from a case-sensitive glob to a smart-case regex (it would be so nice for consistency). |
Unfortunately, that can break programmatic usages of |
Ya, I see it now for env variables but an alias flag should at least be fine :/. Some projects have different commands for the same tool that provides extra/differing functionality. Broot has 'br' and zoxide has 'z' but the original commands can still be used. They also change things and aren't just a shorter invocation of the same thing. So an idea could be to somehow enable a different command to be called entirely that would allow users to more extremely deviate from the default behavior of fd without touching the original command. It could allow for a more obvious indication that this is being called interactively vs programmatically. That it would be more volatile with more users' unique preferences while allowing the original functionality of the fd command to remain consistent and intact. Because this concern of not messing with the original programmatic functionality seems to pop-up across different requests, maybe it could help address this in various areas. At the very least, I do hope a standalone new flag can be added. In these two examples they use shell integrations which sounds very much against this projects ideals of wanting a simple consistent result across OS's and probably not wanting to add a bunch of unique shell integrations for various platforms. So the exact implementation doesn't have to be the same, I'm just speaking more to the concept as a whole. Edit: |
I speak from experience with $ grep foo
$ grep -v foo
$ awk "/foo/"
$ awk "/!foo/" At present it is not possible to use $ fd --type d
2025-03-13 W11 a\
2025-03-14 W11 b\
2025-03-16 W11 c\
2025-03-20 W12 d\
2025-03-24 W13 e\
2025-03-24 W13 f\
2025-03-27 W13 g\
$ fd --type d W13
2025-03-24 W13 e\
2025-03-24 W13 f\
2025-03-27 W13 g\
but the following command does not invert as expected
$ fd --type d -E W13
2025-03-13 W11 a\
2025-03-14 W11 b\
2025-03-16 W11 c\
2025-03-20 W12 d\
2025-03-24 W13 e\
2025-03-24 W13 f\
2025-03-27 W13 g\
so you have to add asterisks
$ fd --type d -E *W13*
2025-03-13 W11 a\
2025-03-14 W11 b\
2025-03-16 W11 c\
2025-03-20 W12 d\ Proposal: make $ fd --type d -EE W13
2025-03-13 W11 a\
2025-03-14 W11 b\
2025-03-16 W11 c\
2025-03-20 W12 d\ |
A short flag needs to be a single letter. |
I noticed
grep
has a-L
flag to find filenames that don't contain the search pattern. What about the related operation of finding the complement of a pattern? Would a flag for that be useful, or is it there some simple way to specify it in the pattern regex?The text was updated successfully, but these errors were encountered: