How to find file with name="php.ini" on linux using grep command ? Can anybody show me ?
Linux – How to find file with name=“php.ini” on linux using grep command
command linegreplinux
Related Solutions
Try
find /srv/www/*/htdocs/system/application/ -name "*.php" -exec grep "debug (" {} \; -print
This should recursively search the folders under application
for files with .php
extension and pass them to grep
.
An optimization on this would be to execute:
find /srv/www/*/htdocs/system/application/ -name "*.php" -print0 | xargs -0 grep -H "debug ("
This uses xargs
to pass all the .php
files output by find
as arguments to a single grep
command;
e.g., grep "debug (" file1 file2 file3
. The -print0
option of find
and -0
option of xargs
ensure the spaces in file and directory names are correctly handled. The -H
option passed to grep
ensures that the filename is printed in all situations. (By default, grep
prints the filename only when multiple arguments are passed in.)
From man xargs:
-0
Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find
-print0
option produces input suitable for this mode.
Are you looking for "howdy doody" in the filename, or within the file?
# find files with "howdy doody" in the filename
find * -name "*howdy doody*" -print0 | xargs -0 ...
xargs
is what you need to use to split the null-terminated output from find -print0
. In your example, the echo
is extraneous; don't bother with it.
# find files containing "howdy doody"
find * -print0 | xargs -0 grep -l "howdy doody"
# find files containing "howdy doody" and do further processing
# multiple xargs version
find * -print0 | xargs -0 grep -l "howdy doody" | xargs -i{} do-something "{}"
# "sh -c" version
find * -print0 | xargs -0 -i{} sh -c 'grep -l "howdy doody" "{}" && do-something "{}"'
# notes:
# "sh -c" allows us to run a complex command with a single xargs
# "{}" handles spaces-in-filename
# handles any &&, ||, | command linking
You can also run your command directly from the find
with -exec
. The difference is that find -exec
runs the command once per file found; xargs
adds filenames to the end of the command (up to system limit), so it runs the command fewer times. You can simulate this with xargs -n1
to force xargs
to only run one command per input.
# grep once per file
find * -exec grep -l "howdy doody" {} \; | ...
Best Answer
You would normally use
find
notgrep
to find files by name.If you must use grep
In both cases replace "/" with the absolute or relative path for the directory you wish to start the search in.
Note that linux also has a
locate
command that relies on indexing - check it's man page for details. This is fastest if the right locations are indexed.