9.7 递归式地搜索文件中的文本

-R

星号(*)通配符可以一次搜索同一目录中的多个文件,但要搜索多个子目录,就需要使用-R(或--recursive)选项。让我们在一组文件中搜索一下hideous这个词,这些文件都是我喜欢的19世纪和20世纪初期的恐怖小说作家的作品,虽然这些小说已经过时了,但仍然很精彩。

  1. $ grep -R hideous *
  2. machen/great_god_pan.txt:know, not in
  3. your most fantastic, hideous dreams can you have
  4. machen/great_god_pan.txt:hideously
  5. contorted in the entire course of my practice, and I
  6. machen/great_god_pan.txt:death was
  7. horrible. The blackened face, the hideous form upon
  8. lovecraft/Beyond the Wall of Sleep.txt:some hideous
  9. but unnamed wrong, which
  10. lovecraft/Beyond the Wall of Sleep.txt:blanket over
  11. the hideous face, and awakened the nurse.
  12. lovecraft/Call of Cthulhu.txt:hideous a chain. I
  13. think that the professor, too, intended to
  14. lovecraft/Call of Cthulhu.txt:voodoo meeting;
  15. and so singular and hideous were the rites
  16. lovecraft/Call of Cthulhu.txt:stated, a very
  17. crude bas-relief of stone, comprising a hideous...

提示 当然,如果显示的结果太多,则应该通过管道把结果输出到less命令,如9.3节中对locate命令的结果进行的处理一样。

  1. $ grep -R hideous | less

另一种办法就是把命令的输出发送到一个文本文件中,然后使用任何你喜欢的文本编辑器打开:

  1. $ grep -R hideous > hideous_in_horror.txt

如果需要把搜索结果保存起来以备后用,这个方法不错。