how to find a word in text files from a directory [duplicate]

Solution 1:

If you don't want to install additional software, you can simply use grep in the terminal. To recursively search through directories, you can use the -r option (see man grep for details). The syntax you are looking for is probably:

grep -r "[STRING TO SEARCH FOR]" "[DIRECTORY TO SEARCH]"

So, for instance if I want to search for the string "asdf" in all files in all subdirectories of "/tmp/testdir/" the command looks like this:

grep -r "asdf" "/tmp/testdir/"

The quotation marks are not strictly necessary, but if your string or directory path contains whitespaces, you otherwise would have to mask them using the \ character...

Solution 2:

  1. Open a terminal.
  2. Install ack-grep by typing sudo apt-get install ack-grep
  3. Change to the directory you want to search under, and type ack-grep foo. it lists out all the matches in all files under that directory.

Solution 3:

You can use recursive grep with the -l flag to only print the file's name instead of the matched line:

grep -Rl foo .

Or, you can use find:

find . -type f -exec grep -l foo {} +

Or. you can use extglob and normal grep

shopt -s extglob
grep -l foo **/*

Solution 4:

Just install gnome-search-tool using sudo apt-get install gnome-search-tool
Search in dash for 'search for files' and launch it.

See image below:

enter image description here

  1. Leave the 'name contains' section empty.
  2. Select your folder.
  3. Unfold the 'select more options' part and choose 'contains the text' and then press add.
  4. Type here the text you want to search for and click find at the bottom right corner.

Enjoy.