Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: grep: find: Is a directory

Tags:

linux

grep

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?

like image 517
Adoyt Avatar asked Jul 12 '17 01:07

Adoyt


People also ask

Can you grep a directory?

To Search Subdirectories To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.

How do I grep from the root directory?

Searching RecursivelyThe command will use grep , the -r flag, the pattern/string you want to search for, and the path of the directory to search in. You can see that the command returned every line that matched the pattern in both the poem and story text file.

How do I grep a filename in a directory?

Conclusion – Grep from files and display the file name grep -n 'string' filename : Force grep to add prefix each line of output with the line number within its input file. grep --with-filename 'word' file OR grep -H 'bar' file1 file2 file3 : Print the file name for each match.

How do I search for a directory in Linux?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How to search all the files in a directory with grep?

If you want to search all the files in a directory with grep, use it like this: There is a problem with it. It only searches in all the files in the current directory. It won't search in the subdirectories. You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option:

How to use grep in Linux?

How to use grep. Without passing any option, grep can be used to search for a pattern in a file or group of files. The syntax is: grep '<text-to-be-searched>' <file/files>. Note that single or double quotes are required around the text if it is more than one word. You can also use the wildcard (*) to select all files in a directory.

What is globally search for regular expression and print out (grep)?

grep stands for Globally Search For Regular Expression and Print out. It is a command line tool used in UNIX and Linux systems to search a specified pattern in a file or group of files.

How do I find a specific text file in a directory?

If you have a bunch of text files in a directory hierarchy, e.g, the Apache configuration files in /etc/apache2/ and you want to find the file where a specific text is defined, then use the -r option of the grep command to do a recursive search.


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

like image 74
Elvis Plesky Avatar answered Oct 21 '22 07:10

Elvis Plesky


The correct answer would be:

grep -d skip alex /etc/*
like image 30
Jens Franik Avatar answered Oct 21 '22 07:10

Jens Franik