0

I'm fairly new to find and regular expressions have always been tricky for me. I am hoping someone can quickly correct this for me and maybe even tell me why this isn't doing that I think it should (exclude files of certain types is the goal):

find . -type f \( -name "*" ! -name "*\.(vm | jar | class | htm | html | jpg | gif)$" \)

Thanks in advance.

Nik
  • 7,113
  • 13
  • 51
  • 80

3 Answers3

2

I'm not familiar with the find command syntax, but to match files with given extension you would need:

".*\.(vm|jar|class|htm|html|jpg|gif)$"

Removed spaces and added dot at the beginning.

Tomek
  • 3,267
  • 2
  • 22
  • 23
2

Looks like the question is about a Unix shell...

Anyway, on the regex side, which I know better, I think you must remove the whitespaces you have in your expression:

"\.(vm|jar|class|html?|jpg|gif)$"

I don't guarantee the validity of your command itself...

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • Not 100% correct, what about file: `handjar` it hasn't got extension but would still match. – Tomek Dec 20 '12 at 15:02
  • Good remark, I edit... Thanks. Not even sure if `find` does a full match or match anywhere. In the first case, your answer is better. – PhiLho Dec 20 '12 at 15:05
1

I'm pretty sure find looks for every names by default, so your extra "-name "*"" is not necessary here.

On the regexp side, it looks like you've found the answer you were looking for, but you might still want to know that find has an OR flag (-or or -o) that can make things easier when regular expressions are causing you troubles:

find . -type f ! \( -name "*.vm" -o -name "*.jar" -o -name ".class" \)

On the downside, it's obviously longer to write...

Remember to take a look at man find.

Good luck

cmc
  • 2,061
  • 1
  • 19
  • 18