3

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

2
  • 2
    To the close-voter, this falls firmly into the realm of shell-programming in my mind. Commented Aug 31, 2010 at 0:21
  • 5
    files 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? Commented Aug 31, 2010 at 1:06

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The OP asked for case insensitive. You forgot -i.
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"

Comments

0
shopt -s nullglob
shopt -s nocaseglob
for file in *foo*
...
...
..

Comments

0

Try:

echo *foo*

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

Comments

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'

Comments

-1

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

4 Comments

that would find FILENAMES that contain "foo", not contained text. (and recurse the directory tree)
and there's no need to use grep.use find's -iname or -name
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
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 *"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.