This tutorial uses the grep command to search strings in files. This tutorial will help you to search all files matching a string recursively.
#HOW TO SEARCH FOR TEXT IN FILES IN CURRENT DIRECTORY LINUX HOW TO#
The output file list includes plain text, scripts, formatted text, and source code files. How to search a directory tree for all files containing specific text strings on Linux using the command line. The grep command needs to be modified accordingly, putting the other character used as a separator in place of “:”. In the rare case in which some of the files contain the character “:” as part of their name, we can use the -F argument of the file command to use some character other than the “:” character as the separator. In other words, the expression means “search for a : in the file command output followed by any number of characters “.*” and then the sub-string ” text”. In this example, we use nix as a search criterion: grep nix The output shows the name of the file with nix and returns the entire line.
The expression “.*” ensures that the sub-string ” text” is only searched in the file type description, not in the filename. To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The subsequent grep command filters all files with “ASCII text” or “UTF-8 Unicode text” as part of the file type description. The file * command prints a list of filenames followed by the file type description of all files in the current directory. We can quickly list all text files in the current directory using a simple command: file * | grep ".* text" If our interest is only in files in the current directory, then the command is effortless.