How to search directory for specific files?

What is the Linux command to search for a word in list of files in a directory?

  • I want to search word  in all the files of a directory and list the files which contain that word. Which linux command will be helpful ?

  • Answer:

    You probably want to use grep. grep -R --color "magic"  directory/ Passing in -R will tell grep to read all files under each directory, recursively. If your console support coloring, you definitely want to use --color. It highlights the word you are looking for.

Joe Tyson at Quora Visit the source

Was this solution helpful to you?

Other answers

No. In order to associate a file or directory with a process which created or wrote to it one would have to modify both the kernel and the filesystem to accommodate this additional data ... or one would have to be running a process which was explicitly monitoring the parent directory using something like inotify or dnotify.  Such a monitoring program would have to have been running at the time of the creation/modification. It's possible that the necessarily customizations might be easy to implement using existing tracing and auditing features which are available in the kernel source tree (though not necessarily compiled into the kernels used by your distribution. (For example one can imagine a kernel patch which added or modified an "extended attribute" to a file to store process information immediately after create or modify --- or one might have this meta-data emitted as an event into something like the kernel printk() ring buffer such that some user space process could consume and log these into some central storage).

Jim Dennis

grep -w -l <searchword> <pathofdirectory> the -w option forces grep to exactly match the word, -l option gives only the filenames grep -w -l -r <searchword> <pathofdirectory> the -r option if you want to search the sub-directories also Frequently used grep Options –color highlight the matched pattern with color -i ignore case -v matches everything other than pattern -r recursively search all files in specified folder(s) -n print also line number(s) of matched pattern -c count of number of ‘lines’ having the match -l print only the filename(s) with matching pattern -L print filename(s) NOT having the pattern -w match exact word ^ match from start of line $ match end of line -A n --> print n number of lines after matching line -B n --> print n number of lines before matching line -C n --> print n number of lines around matching line Further Reading http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/ http://www.panix.com/~elflord/unix/grep.html http://unix.stackexchange.com/questions/17949/what-is-the-difference-between-grep-egrep-and-fgrep https://github.com/learnbyexample/scripting_course/blob/master/Linux_curated_resources.md

Sundeep Agarwal

To list just the file names and not the matches themselves, this works: grep "word" directory/* |awk -F ':' '{print $1}'|sort -u replace "directory" with * to search multiple directories. Update: As Christian and Matthew note, it's better to refrain from abusing awk & sort like I do and instead do: grep -Rl "search-term" *

Jen Harvey

Use grep -ri word * in the current directory where you want to look. Will follow recursively the subdirectories.

Michael Boelen

Below answers are good. Just to add a point, if the search is to be recursive, use grep command with find (pipe), if its not recursive and you want search to restrict to current directory, a normal grep would do. Some Linux systems allow -r option for grep, which would do a recursive search for pattern.

Chirag Deshpande

use the following command: find <path to directory>/*.txt | grep <word> (enter)

Anoop Khandelwal

grep -l "word" * does what you need - prints just the file names.

Alexander Lukyanov

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.