fd
fd copied to clipboard
Negating patterns: add --exclude-regex?
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?
Is there really a strong use case for this?
We already have the --exclude <pattern>
option which lets you specify a glob pattern that should be excluded. While not exactly the same, I'm not sure if there is a need for an option that would invert the pattern.
or is it there some simple way to specify it in the pattern regex?
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 fd | grep -v pattern
.
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 --invert
option would probably help in most use cases:
fd 'keep' --invert --exec rm {}
@SicariusNoctis --exclude
would work for your case as well:
fd -E 'keep*' -X rm
@SicariusNoctis
--exclude
would work for your case as well:fd -E 'keep*' -X rm
@sharkdp Why not add an --exclude-regex
?
I too would like an --exclude-regex
. I am extremely familiar with regex, but not at all with glob patterns. And since fd
does not support negative lookahead, it's impossible to do that kind of thing in a regex pattern.
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 __init__.py
file from the root folder. I have to use something like: fdfind --type file | rg -v '^\./__init__.py'
cause the glob pattern excludes all __init__.py
from all folders.
@razcore-rad I'm pretty sure you could use --exclude=/__init__.py
for that. (note that --exclude
actually uses the same syntax as .gitignore).
@razcore-rad I'm pretty sure you could use
--exclude=/__init__.py
for that. (note that--exclude
actually uses the same syntax as .gitignore).
I see. That's handy. I looked at the man page but it didn't mention the specific syntax. I did try --exclude './__init__.py
, but that didn't help, /__init__.py
works for my use case, thanks.
It uses the same syntax as .gitignore
ripgrep supports this as well within --glob
:
Precede a glob with a ! to exclude it.
@M1cha fd
already has an --exclude
flag that uses globs. This issue is for excluding using a regex pattern rather than a glob pattern.