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...
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 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.
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.
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