May be the question was confusing, i hope its clear now, the patten should match the content not the filename !
Your question was not clear, and in the original answer I have shown how to find filenames matching a pattern. If you only want to search for files with content matching a pattern, it's as simple as
grep 'pattern' directory/*
(the shell globbing is used).
You can still use find
to filter out the files before passing to grep
:
find 'directory' -maxdepth 1 -mindepth 1 -type f \
-exec grep --with-filename 'pattern' {} +
Original Answer
Grep is not appropriate tool for searching for filenames, since you need to generate a list of files before passing to Grep. Even if you get the desired results with a command like ls | grep pattern
, you will have to append another pipe in order to process the files (I guess, you will most likely need to process them somehow, sooner or later).
Use find
instead, as it has its own powerful pattern matching features.
Example:
find 'directory' -maxdepth 1 -mindepth 1 -regex '.*pattern'
Use -iregex
for case insensitive version of -regex
. Also read about -name
, -iname
, -path
, and -ipath
options.
It is possible to run a command (or script) for the files being processed with -exec
action, e.g.:
find 'directory' -maxdepth 1 -mindepth 1 -type f \
-regex '.*pattern' -exec sed -i.bak -r 's/\btwo\b/2/g' {} +