开发者

grep on some files

I want to find all 开发者_StackOverflow中文版files with extension x in all sub folders containing string s, how do I do this?

grep -nr s .*.x ?????

Dirk


GNU find

find . -iname "*.x" -type f -exec grep -l "s" {} +; 

If you have Ruby(1.9+)

Dir["/path/**/*.x"].each do |file|
  if test(?f,file)
   open(file).each do |line|
     if line[/s/]
       puts "file: #{file}"
       break
     end
   end
  end
end


I would first find the *.x files, and then search the string you are interested in with grep:

$ find directory -name "*.x" -exec grep -Hn s {} \;
  • -name "*.x" searches recursively every file sufixed with x.
  • -exec grep ... {} \; searchs the s string for each encountered file.
  • -H is recommended, since you wouldn't know which file matched the expression.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜