4

Background: I'm trying to get a total line count of all of the code files (.html|.htm|.php|.js|.css) in my root web dir (recursively) by piping the output of this into xargs wc -l | grep total.

$ find . -regex '.+\.php'
./inc/db.php
./inc/userauth.php
./index.php
.......... etc.

$ find . -regex '.+\.js'
./inc/jquery-1.7.1.js
./inc/script.js

$ find . -regex '.+\.(php|js)'
(returns nothing)

According to this,

abc(def|xyz) matches abcdef or abcxyz

So shouldn't .+\.(php|js) match all .php files and .js files?

4
  • 1
    You need to escape certain characters that interpreted by the bash shell. You can find a list of those characters here: grymoire.com/Unix/Quote.html Commented Feb 23, 2012 at 20:51
  • 2
    @NickGarvey: I thought those characters would not have any special meaning when used in single quotes. Commented Feb 23, 2012 at 20:55
  • The do not have any special meaning when in single quotes. The issue as explained in the answers is that escaping is required for certain characters when not using ERE. By default GNU find uses emacs regex, which requires the escaping. Commented Feb 23, 2012 at 22:10
  • Ah you are right, it is regex syntax as mentioned in the answers. My mistake. Commented Feb 23, 2012 at 22:10

2 Answers 2

6

find uses a different style of regex, so you have to write \(js\|php\) instead of just (js|php).

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Alternatively, you can instruct find to use EREs (which are more like the regexes the OP has in mind) by writing find . -regextype posix-extended -regex '.+\.(php|js)'.
.. and mac needed the option -E for EREs
4
find . -regex '.+\.\(php\|js\)'

Escape characters which are special, although it does depend on your shell (so I've been zealous here).

Comments

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.