I want to search for a string of text in all files in a directory (and not its subdirectories; I know the -r
option does that, but that is not what I want).
Running
grep "string" /path/to/dir
is supposed to be able to do this, I've read, but it gives me the error:
grep: dir: Is a directory
Next, I tried running
grep
on multiple files.grep "string" .bashrc .bash_aliases
works perfectly.grep "string" .bash*
works as intended too.grep "string" *
gives me the errors:grep: data: Is a directory grep: Desktop: Is a directory grep: Documents: Is a directory grep: Downloads: Is a directory ...
Only the errors are printed, I don't get the matching lines. I tried using the -s
option, but to no avail.
So, my questions:
Why am I not being able to use
grep
on a directory, as in (1), when I should be able to? I've seen that done in plenty examples on the Internet.
Edit: When I say "using grep on a directory", I mean "search in all the files in that directory excluding its subdirectories". I believe that this is what grep does when you pass a directory to it in place of a file. Am I incorrect?Please give me an explanation on the workings of
grep
that would explain the behavior of commands in (2).
Edit: Let me be more specific. Why does using wildcards to specify multiple files to search in for work with.bash*
and not with*
or even./*
?How can I search all the files in a directory (and not its subdirectories) using
grep
?
In Bash, a glob will not expand into hidden files, so if you want to search all the files in a directory, you need to specify hidden files
.*
and non-hidden*
.To avoid the "Is a directory" errors, you could use
-d skip
, but on my system I also get an errorgrep: .gvfs: Permission denied
†, so I suggest using-s
, which hides all error messages.So the command you are looking for is:
If you are searching files in another dir:
Another option is to use the
dotglob
shell option, which will make a glob include hidden files.For files in another dir:
† Someone mentioned that I shouldn't get this error. They may be right - I did some reading but couldn't make heads or tails of it myself.
You need the
-d skip
option added on.Grep is searching inside of files. You can search recursively, as you said, if you want to search files inside of a directory.
By default, grep will read all files, and it detects the directories. Because by default you have not defined what to do with the directories with the
-d
option, it give error output.Searching just within the parent directory would be
grep -d skip "string" ./*
Old timers would probably do this:
Rephrasing - you want to grep the files in one level of subdirectory, but not recurse though all sub-sub directories?
Or if you don't want the files in the current directory
Note this won't find directories starting with a dot.
should do that job.
There's also
-maxdepth
and-mindepth
restriction parameters available to thefind
command too.You can think like this, for example using grep.
So this search for string "PATH" listing name of the files below the user's home directory, only for files that start with a dot .
Using grep PATH ~/.[^.]* you'll see all occurrence, including line with searching keyword.
To get rid of error redirect to /dev/null for example
So you can apply this pattern for searching "Apache" string in files from /etc directory-looking only in files below this main directory. You see that this don't return from /etc/httpd/conf/httpd.conf
Here is an example to properly skip directories without hiding all errors (without
-s
param on chosen answer):