0

I search SO for something that might give me this simple answer and it seems to be here and here (among many) but I simply can not get it to work.

I have files like

12_pattern1.yyy_zzz #find
13_pattern1.xxx.pattern2_xx 
12_yy_pattern1:xxx
14_pattern1.xxx_pattern2.xxx #find
12_xxx_zzz.yyy.xxx
14_pattern1.xxx.yyy #find

I need to find and remove files that start with 12 or 14, have _pattern1 immediately hereafter and do NOT end with pattern2.

I tried something along find . -regex ".*/\(12_\|14_\)pattern1.*[^pattern2$]" -exec rm -f {} \;

What am I missing here ?

2 Answers 2

3

How about something like this:

find . -regex "^\(12_\|14_\)pattern1.*" -not -regex "*.pattern2$"
Sign up to request clarification or add additional context in comments.

2 Comments

Ah yes! This is perfect - I was trying to get it into the the one regex (just to be annoying: can you put the -not into the first regex as I tried?)
You can have a negated character class, but you can't simply negate a pattern in a regular expression. As far as i'm aware this is the only reasonable way to do it.
1

find command supports posix-* flavors for Regular Expressions. It means you can't do it simply with find. However, if you could use grep then you could do it easily (but with help of two tools):

find . | grep -P '(14|12)_pattern1[^/]*$(?<!pattern2)'

2 Comments

Then to remove the found files could this be possible as in -exec rm {} \; with find
Aaw I didn't look at following options carefully. No it's not possible this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.