How to write a bash command that finds all files in the current directory that contain the word “foo”, regardless of case?
Asked
Active
Viewed 238 times
3
-
2To the close-voter, this falls firmly into the realm of shell-programming in my mind. – ysth Aug 31 '10 at 00: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 Aug 31 '10 at 01:06
6 Answers
2
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
.

bitmask
- 32,434
- 14
- 99
- 159
1
Assuming you want to search inside the files (not the filenames)
If you only want the current directory to be searched (not the tree)
grep * -nsie "foo"
if you want to scan the entire tree (from the current directory)
grep . -nsrie "foo"

ocodo
- 29,401
- 18
- 105
- 117
0
Try:
echo *foo*
will print file/dir names matching *foo*
in the current directory, which happens to match any name containing 'foo'.

hasen
- 161,647
- 65
- 194
- 231
0
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'

icco
- 3,064
- 4
- 35
- 49
-1
find . -type f | grep -i "foo"

dj_segfault
- 11,957
- 4
- 29
- 37
-
that would find FILENAMES that contain "foo", not contained text. (and recurse the directory tree) – ocodo Aug 31 '10 at 00:50
-
-
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 Aug 31 '10 at 01:14
-
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 *" – dj_segfault Aug 31 '10 at 01:20