How can I find all files containing specific text (string) on Linux?
How can I find all files containing specific text (string) on Linux?
27014-Jul-2023
Home / DeveloperSection / Forums / How can I find all files containing specific text (string) on Linux?
Aryan Kumar
15-Jul-2023Sure, there are a few ways to find all files containing specific text on Linux. Here are two methods:
Method 1: Using the
find
commandThe
find
command is a command-line tool that can be used to find files and directories. To find all files containing specific text, you can use the following syntax:For example, to find all files containing the text "hello" in the current directory, you would run the following command:
The
-name
option tells thefind
command to only search for files that match the specified name pattern.The
-exec
option tells thefind
command to execute the specified command for each file that is found.The
grep
command is a command-line tool that can be used to search for text in files. The-i
option tells thegrep
command to ignore case when searching for text.Method 2: Using the
grep
commandThe
grep
command is a command-line tool that can be used to search for text in files. To find all files containing specific text, you can use the following syntax:For example, to find all files containing the text "hello" in the current directory, you would run the following command:
The
-i
option tells thegrep
command to ignore case when searching for text.The
<directory_path>
parameter is the path to the directory that you want to search.The
*.txt
pattern tells thegrep
command to only search for files that have the.txt
extension.