tags:

views:

99

answers:

6
+3  Q: 

unix bash command

How to write a bash command that finds all files in the current directory that contain the word “foo”, regardless of case?

A: 

find . -type f | grep -i "foo"

dj_segfault
that would find FILENAMES that contain "foo", not contained text. (and recurse the directory tree)
slomojo
and there's no need to use grep.use find's -iname or -name
ghostdog74
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 *"
dj_segfault
A: 
shopt -s nullglob
shopt -s nocaseglob
for file in *foo*
...
...
..
ghostdog74
+2  A: 

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
The OP asked for case insensitive. You forgot `-i`.
Dennis Williamson
Thanks. Changed.
bitmask
A: 

Try:

echo *foo*

will print file/dir names matching *foo* in the current directory, which happens to match any name containing 'foo'.

hasen j
+1  A: 

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"
slomojo
A: 

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