简体   繁体   中英

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

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 .

正确答案是:

grep -d skip alex /etc/*

Setting the environmental variable GREP_OPTIONS to include the value "--directories=skip" should suppress that “Is a directory” message (ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM