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 error grep: .gvfs: Permission denied
†, so I suggest using -s
, which hides all error messages.
So the command you are looking for is:
grep -s "string" * .*
If you are searching files in another dir:
grep -s "string" /path/to/dir/{*,.*}
Another option is to use the dotglob
shell option, which will make a glob include hidden files.
shopt -s dotglob
grep -s "string" *
For files in another dir:
grep -s "string" /path/to/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.
find . -not -path './flash_drive_data*' | grep "./*flash*"
The thing here is that grep
uses regular expressions, while find -path
uses shell glob style pattern matches. The asterisk has a different meaning in those two.
The regular expression ./*flash*
matches first any character (.
), then zero or more slashes (/*
), then a literal string flas
, then any number (zero or more) of h
characters. 3/flas
matches that (with zero times h
), and so would e.g. reflash
(with zero times /
).
You could just use grep flash
instead, given that it matches anywhere in the input, so leading and tailing "match anything" parts are unnecessary.
Or use find -path './*flash*' -and -not -path './flash_drive_data*'
When I replaced grep "*flash*"
with just grep "*"
, I got [no matches].
Since the asterisk means "any number of the previous atom", it's not really well defined here. grep
interprets that as a literal asterisk, but really it should be an error.
However, when I ran: find . -not -path './flash_drive_data*' -exec tar cfv home.tar.bz '{}' +
I was getting output including things like:
./flash_drive_data/index2/ask-sdk-core/dist/dispatcher/error/handler/
so flash_drive_data
files were being included.
Note that tar
stores files recursively, and the first output of that find
is .
for the current directory, so everything will be stored. You may want to use ! -type d
with find
to exclude directories from the output, or (better), look at the -exclude=PATTERN
options to tar
.
Best Answer
From
man grep
(emphasis mine):If you ran
grep
without a filename, or a pipe (the|
inls | grep
):The standard input is the terminal - i.e., you. You have to provide the input which
grep
will search.