I was wondering what grep -v "grep"
does and what it means?
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
?
Here's my command that I'm using in a script to grep
real-time data. It doesn't seem to pull real-time data correctly as it just misses some lines.
tail -f <file> | fgrep "string" | sed 's/stuff//g' >> output.txt
What would the following command do? What is "line buffering"?
tail -f <file> | fgrep --line-buffered "string" | sed 's/stuff//g' >> output.txt
This
ls -l /var/log | awk '{print $9}' | grep "^[a-z]*\.log."
outputs this:
alternatives.log.1
alternatives.log.10.gz
alternatives.log.2.gz
alternatives.log.3.gz
alternatives.log.4.gz
alternatives.log.5.gz
alternatives.log.6.gz
alternatives.log.7.gz
alternatives.log.8.gz
alternatives.log.9.gz
apport.log.1
apport.log.2.gz
apport.log.3.gz
but this:
ls -l /var/log | awk '{print $9}' | grep "^[a-z]+\.log."
outputs nothing.
Why? I just changed *
to +
. Isn't it similar? Operator +
just needs at least one match, and *
zero or more.
How can I recursively search for directory names with a particular string where the string is only part of the directory name?
For example: the directory name is "8.0.3-99966_en", but I want to recursively search for directories with the string "99966".