如何查找包含特定单词的文本文件(不在其名称中)

我想在我的硬盘中找到一个包含特定单词的文本文件。

在Ubuntu 12.4之前,我曾经在破折号中启动一个应用程序,我认为它被称为“搜索文件……”,其图标是放大镜。我再也找不到那个简单的应用程序了。

您可以使用终端的grep命令:

  grep -r word * 

此命令将在当前目录(或子目录)下的所有文件中查找所有出现的“word”。

安装gnome-search-tool。

 sudo apt-get install gnome-search-tool 

打开Search for files选择Select More Options


在此处输入图像描述

问题很老……无论如何……目前(2016年)有一个叫做tracker的gnome应用程序(你可以在ubuntu存储库中找到它),可以安装它来搜索文件里面的文本(试过odt-ods-odp-pdf )。 该软件包附带4个其他软件包(Tracker-extract,tracker-gui,tracker-miner-fs,tracker-utils)Namastè:)

以下是可用于搜索特定文本字符串的文件的不同方法的概述,其中一些选项专门用于处理文本文件,并忽略二进制/应用程序文件。

然而,应该注意的是,搜索单词可能会有点复杂,因为大多数行匹配工具会尝试在行上的任何位置找到单词。 如果我们将一个单词称为字符串,可以出现在行的开头或结尾,或者单独在行上,或者用空格和/或标点符号包围 – 那就是我们需要正则表达式时,特别是那些来的来自Perl。 例如,我们可以在grep使用-P来使用Perl正则表达式来包围它。

 $ printf "A-well-a don't you know about the bird?\nWell, everybody knows that the bird is a word" | grep -noP '\bbird\b' 1:bird 2:bird 

简单的grep

 $ grep -rIH 'word' 
  • -r用于从当前目录向下递归搜索
  • -I忽略二进制文件
  • -H输出找到匹配项的文件名

仅适合搜索。

找到+ grep

 $ find -type f -exec grep -IH 'word' {} \; 
  • find递归搜索部分
  • -I选项是忽略二进制文件
  • -H输出找到行的文件名
  • 与子shell中的其他命令组合的好方法,例如:

     $ find -type f -exec sh -c 'grep -IHq "word" "$1" && echo "Found in $1"' sh {} \; 

Perl的

 #!/usr/bin/env perl use File::Find; use strict; use warnings; sub find_word{ return unless -f; if (open(my $fh, $File::Find::name)){ while(my $line = <$fh>){ if ($line =~ /\bword\b/){ printf "%s\n", $File::Find::name; close($fh); return; } } } } # this assumes we're going down from current working directory find({ wanted => \&find_word, no_chdir => 1 },".") 

在递归bash脚本中的穷人递归grep

这是“抨击方式”。 如果安装了grepperl ,可能没有理由使用它。

 #!/usr/bin/env bash shopt -s globstar #set -x grep_line(){ # note that this is simple pattern matching # If we wanted to search for whole words, we could use # word|word\ |\ word|\ word\ ) # although when we consider punctuation characters as well - it gets more # complex case "$1" in *word*) printf "%s\n" "$2";; esac } readlines(){ # line count variable can be used to output on which line match occured #line_count=1 while IFS= read -r line; do grep_line "$line" "$filename" #line_count=$(($line_count+1)) done < "$1" } is_text_file(){ # alternatively, mimetype command could be used # with *\ text\/* as pattern in case statement case "$(file -b --mime-type "$1")" in text\/*) return 0;; *) return 1;; esac } main(){ for filename in ./**/* do if [ -f "$filename" ] && is_text_file "$filename" then readlines "$filename" fi done } main "$@" 

是的,我知道你正在寻找gui申请,这是老post,但也许这有助于某人。 我找到了ack-grep util。 首先通过sudo apt-get install ack-grep安装它,然后在要搜索的目录中运行命令ack-grep what_you_looking_for 。 这将显示您的文本中的所有文件,并显示此文件的预览。 这对我来说非常重要。

更简单,更快捷的是“silverlight搜索者” sudo apt-get install silversearcher-ag 。 看看https://github.com/ggreer/the_silver_searcher ,了解为什么它比ack *更好。