fd icon indicating copy to clipboard operation
fd copied to clipboard

Negating patterns: add --exclude-regex?

Open alok opened this issue 7 years ago • 22 comments

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?

alok avatar Dec 13 '17 00:12 alok

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.

sharkdp avatar Dec 14 '17 03:12 sharkdp

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 {}

YodaEmbedding avatar Jan 16 '20 14:01 YodaEmbedding

@SicariusNoctis --exclude would work for your case as well:

fd -E 'keep*' -X rm

sharkdp avatar Jan 16 '20 19:01 sharkdp

@SicariusNoctis --exclude would work for your case as well:

fd -E 'keep*' -X rm

@sharkdp Why not add an --exclude-regex?

NightMachinery avatar Apr 29 '21 15:04 NightMachinery

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.

raiguard avatar May 19 '21 04:05 raiguard

Ok, reopening for now.

sharkdp avatar May 21 '21 12:05 sharkdp

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 avatar Dec 22 '22 14:12 razcore-rad

@razcore-rad I'm pretty sure you could use --exclude=/__init__.py for that. (note that --exclude actually uses the same syntax as .gitignore).

tmccombs avatar Dec 22 '22 18:12 tmccombs

@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.

razcore-rad avatar Dec 23 '22 05:12 razcore-rad

It uses the same syntax as .gitignore

tmccombs avatar Jan 26 '23 23:01 tmccombs

ripgrep supports this as well within --glob:

Precede a glob with a ! to exclude it.

M1cha avatar Apr 21 '23 06:04 M1cha

@M1cha fd already has an --exclude flag that uses globs. This issue is for excluding using a regex pattern rather than a glob pattern.

tmccombs avatar Apr 21 '23 18:04 tmccombs