Signup/Sign In
LAST UPDATED: OCTOBER 12, 2021

How to Search Files containing a Text in Linux

Technology #Linux#HowTo

    In Linux, you have to use commands to do everything. And most of us are generally not very good with these commands, and to search any file we go around looking for that file in different directories using the cd.. command.

    Not anymore! If you are here, that means you decided to look for a command that can help you search for files using some specific text that the file contains.

    Linux provides many ways of doing so. like the grep command, find command, fgrep command, etc. In this tutorial we will use the grep command as that is the most popular one and can be used in many different ways for searching.

    Search Files containing specific Text using grep:

    If you are in the directory in which the files are present, and there are only files in the directory, no sub-directory, then you can simply run the following command to search:

    grep 'text to search' *

    The last asterix(*) is used to specify that search in all the files. We can specify a pattern such as *.py to search only the files with extension .py or *.java to search in files with extension .java, etc.

    grep 'text to search' *.py

    Search Recursively in Directories

    If you have to search for files containing some specific text in a directory, which may have sub-directory with files or more sub-directories, then we can do so by using the recursive flag with the grep command.

    grep -r 'test to search' *

    -r or -R is for recursive search in all the directories in the current directory.

    Other grep Search Options:

    If we want to display the line number on which the text is found, then add the line number flag too:

    grep -rn 'test to search' *

    -n is to show the line number.

    If we wish to match the whole text, then we can add the -w flag and to display the name of files only in which the text is found then add the -l(lower case L) option.

    Combining all the options:

    grep -rnwl 'test to search' *

    It is not necessary to be in the directory in which you want to search. We can provide the path of the directory too, like:

    grep -rl '/path/to/somewhere/' -e 'text to search'
    # or simply
    grep -rl 'text to search' /path/to/somewhere/

    Ignore Search Text case:

    This is also a very important usecase. If you wish to search for some text, and are not sure of the exact term, then you can add the ignore case option to the grep command.

    grep -ri 'text to search' *

    -i is the option used for specifying ignore case for the text to be searched.

    Exclude some files from Search

    If you want to exclude some files from search, like some file with .py extension, then you can do so by using the --exclude option with the grep command:

    grep --exclude==*.py -ril 'text to search' *

    The above command will search all the files present in the current directory, except the files with the .py extension.

    Exclude Directory from Search

    If there are some sub-directories present in the main directory that you are searching, and you do not wish to search them, you can exclude them using the --exclude-dir option:

    grep --exclude-dir=={dir1,dir2} -ril 'text to search' *

    The above command will exclude the directories dir1 and dir2 from the search.

    Conclusion:

    So many ways to search using the grep command. So if you have to search for any text in files, be it 10 files or 100 files, all you have to do is use the grep command and search. If you face any issue, or there is some specific usecase which is not covered above, do share it with us by posting your valuable comment and we will definitely address it.

    You may also like:

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS