开发者

Better way to limit the unix command find by filename

I'm getting results using find with filenames that have '~' and .swp, etc. So I did the following, but is there a bette开发者_StackOverflowr way to do this? The '.*.js' -iname '*.js' part feels "redundant".

$ find ./ '.*.js' -iname '*.js' -print0 | xargs -0 grep -n ".*loginError.*"
find: `.*.js': No such file or directory
./js/signin.js:252:                               foo.loginError();
./js/signin.js:339:foo.loginError = function() {
./js/signin.js:340:    foo.log("ui.loginError");


Try using

find . -name \*.js -print0 | xargs -0 grep -n ".*loginError.*"

That will find only files with 'js' extension and not ending in ~ or .swp

EDIT: Added '0' -print0 (edit requires 6 characters so I'm adding this; ergh!)


To do it all in one command without the xargs you could do it like this

find . -name "*.js" -exec grep -n ".*loginError.*" /dev/null {} \;

the /dev/null piece is to make grep think it's searching multiple files and then it'll output the filename correctly, otherwise it'd just print out the line number without telling you which file it's in

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜