Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When using grep -r you could search in all files with either * or . and it seems to return the same thing but is it really the same?

Let's say I search for "foo", then I could write

grep -r foo *

or

grep -r foo .

Would anyone try to explain the difference between . and *?

share|improve this question
add comment

2 Answers 2

up vote 15 down vote accepted

grep -r foo * doesn't look for matches in hidden files or directories, also * is expanded by the shell so you might end up with an Argument list too long error when there are a lot of entries in the current directory, or some other errors or misbehaviour if the name of some of the files or directories starts with a dash character.

Invocation grep -r foo . doesn't have the above flaws

Updated:

Another difference: grep's man page (@fedora17) says:

-r, --recursive
    Read all files under each directory, recursively, following symbolic links only if they
    are on  the  command  line.  ...

There will be also a difference when you execute this command in an empty directory:

$ grep -r foo *; echo $?
grep: *: No such file or directory
2
$ grep -r foo .; echo $?
1
$
share|improve this answer
add comment

If you use asterisk, you cannot match files in directories whose name start with a dot, like .cache.

Update:

This is because the * is expanded by the shell before calling grep, so it receive a list of names instead of a single directory name (for the current .). The way shell expand the pattern may be customized with shell parameter (as for nullglob, nocaseglob or other bash options) or be environment variables (like LANG or LC_ALL)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.