限制unix命令按文件名查找的更好方法
我使用 find 和包含“~”和 .swp 等的文件名得到结果。所以我执行了以下操作,但是有更好的方法吗? '.*.js' -iname '*.js'
部分感觉“多余”。
$ 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");
I'm getting results using find with filenames that have '~' and .swp, etc. So I did the following, but is there a better 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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
这只会找到扩展名为“js”且不以 ~ 或 .swp 结尾的文件
编辑:添加“0”-print0 (编辑需要 6 个字符,所以我添加此;呃!)
Try using
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!)
要在没有 xargs 的情况下在一个命令中完成这一切,您可以这样做,
/dev/null 部分是让 grep 认为它正在搜索多个文件,然后它会正确输出文件名,否则它只会打印出该行数字而不告诉你它在哪个文件中
To do it all in one command without the xargs you could do it like this
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