

If you require following all symbolic links, use -R instead of -r. r stands for recursive, reading all the files in the directory and its subdirectories. This includes strings like texting for example, because it contains our search pattern, text. Now this gives me the locations of that string. grep -r 'text' This lists all the files in the current folder and subfolders containing text.
For this I used grep 'string' -obI want to search a small string in a large string and find the locations of the string.
Linux search for text in files subdirectories code#
Running the above code gives us the following. Search a string and display its location on the entire string and make a text file. egrep -r 'word1word2' directory-path/ Example egrep -r 'configcomma' hadoop-2.6. In the below example we are searching for files containing either the word config or the word comma. type f -name "*.java" -exec grep -il 'foo' \ įinally, if your know the string is in a file in the current directory there's no need to use the find command. We can also search for multiple words by using the egrep command with character.

When this happens, and other search means like the "locate" command don't help, my favorite way of searching for text strings in files that are spread through a bunch of directories and sub-directories is this:įind. It goes without saying that every good Linux desktop environment offers the ability to search your file system for files and folders.

For more great SysAdmin tips and techniques check out our free intro to Linux course. Linux find/grep FAQ: How can I combine the Linux find and grep commands to search a large collection of files?Ī lot of times when I need to find a file I know the text in the file that I'm looking for, but I can't remember the filename, or can't think of what directory it might be in, other than somewhere below my home directory. This is a classic article written by Jack Wallen from the archives.
