How to write a bash command that finds all files in the current directory that contain the word “foo”, regardless of case?
-
2To the close-voter, this falls firmly into the realm of shell-programming in my mind.ysth– ysth2010-08-31 00:21:22 +00:00Commented Aug 31, 2010 at 0:21
-
5files that contain "foo", files that contain the WORD foo, or filenames that contain foo? and by current directory, do you mean just in the current directory, or the current directory & all subdirectories? what about hidden files, do you want to search those too?George– George2010-08-31 01:06:13 +00:00Commented Aug 31, 2010 at 1:06
Add a comment
|
6 Answers
If you want "foo" to be the checked against the contests of the files in ., do this:
grep . -rsni -e "foo"
for more options (-I, -T, ...) see man grep.
1 Comment
Dennis Williamson
The OP asked for case insensitive. You forgot
-i.I've always used this little shell command:
gfind () { if [ $# -lt 2 ]; then files="*"; search="${1}"; else files="${1}"; search="${2}"; fi; find . -name "$files" -a ! -wholename '*/.*' -exec grep -Hin ${3} "$search" {} \; ; }
you call it by either gfind '*php' 'search string' or if you want to search all files gfind 'search string'
Comments
find . -type f | grep -i "foo"
4 Comments
ocodo
that would find FILENAMES that contain "foo", not contained text. (and recurse the directory tree)
ghostdog74
and there's no need to use grep.use find's -iname or -name
dj_segfault
If you want to wrap it in a script so you can just type "findnocase foo", it should look like this: find . -type f | grep -i $1
dj_segfault
ghostdog is right. It can be simplified as "find . -maxdepth 1 -type f -iname $1". If slomojo is right and you mean you want to search the contents of the file, then all you need is "grep -i $1 *"