背景:我試圖在我的根網站中獲得所有代碼文件(.html | .htm | .php | .js | .css)的總計數dir(遞歸)通過管道輸出到xargs wc -l | grep total
。regex/linux查找命令不能按預期工作
$ 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)
據this,
abc(def|xyz) matches abcdef or abcxyz
所以不宜.+\.(php|js)
匹配所有.php文件和.js文件?
您需要轉義某些由bash shell解釋的字符。你可以在這裏找到這些字符的列表:http://www.grymoire.com/Unix/Quote.html – 2012-02-23 20:51:44
@NickGarvey:我認爲這些字符在單引號中使用時沒有任何特殊含義。 – 2012-02-23 20:55:21
單引號時沒有任何特殊含義。答案中解釋的問題是,在不使用ERE時某些字符需要轉義。默認情況下GNU find使用emacs正則表達式,它需要轉義。 – jordanm 2012-02-23 22:10:25