Error: Grep: Find: Is a Directory

I am new to Linux and going through some tutorials and samples. I created a file called test and put alex and alexander in it. I'm trying to find instances of just alex.

If I do grep alex * I get the error:

grep: find: Is a directory.

If I do cat test | grep alex then I get (as expected)

alex
alexander (with alex in red)

Why does the first cause an error, and the second produce expected results?

5

5 Answers

If you want to grep phrase from specific file use:

# grep "alex" test

In case you use grep alex * it will search through all files inside the current work directory. In case subdirectory will be met it will tell you something like grep: find: Is a directory
If you want to perform a recursive search use -r key. For example

# grep -r "alex" /some/folder/

In this case all files and files inside subdirectories from /some/folder/ will be checked.
And you can always use man grep.

3

The correct answer would be:

grep -d skip alex /etc/*

Setting the environmental variable GREP_OPTIONS to include the value "--directories=skip" should suppress that “Is a directory” message (i.e. enter GREP_OPTIONS="--directories=skip" or add that line to one of your login initialization files like .bashrc to make that behavior permanent).

> cat test
alex
alexander

> grep alex *    
grep: mysql_data: Is a directory
grep: sql_updates: Is a directory
test:alex
test:alexander
grep: tmp_files: Is a directory

> GREP_OPTIONS="--directories=skip"

> grep alex *                      
test:alex
test:alexander

Also since there is a command named “test” and another named “find” it’s usually not advisable to use those as file or directory names. That shouldn’t be a problem here but it can lead to other conflicts.

1

If you wanted to go through all the directories including the find directory, you can run this command:

grep alex * -r

Which will go through all the directories in your current directory recursively.

Or using grep with -l flag or -w flag

$ grep -l alex /etc/* 

To return list of files containing word "alex" in the /etc directory

$ grep -w alex /etc/*

To return list of lines from files in the /etc directory containing "alex" word

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest