I'm trying to understand why using `ls -a | grep .*` doesn't display `.` (current directory) and `..` (parent directory) when these entries should be included in the output of `ls -a`. I know that `ls -a` lists all files, including `.` and `..`, but it seems that when I pipe this output into grep, those two entries are filtered out, almost as if I were using `ls -A`. Is grep handling the pattern in a way that prevents it from showing `.` and `..`? I want to make sure I'm not missing something obvious here while I'm learning.
4 Answers
You should definitely wrap your pattern in single quotes like `ls -a | grep '.*'`. This may help you see what you’re looking for without getting other unintended results from the shell.
Grep doesn’t use the same pattern matching as shell globbing; it employs regular expressions instead. For your case, try using `grep '..*'` so that it properly matches the patterns you want. Just remember to include the quotes to prevent the shell from interpreting it first.
I get the need for quotes now, but why exactly do we need to quote the pattern when using grep?
I initially wanted to exclude `.` and `..`, that's why I used a different command. It's all getting a bit mixed up for me.
To clarify: both `ls -a` and `ls -A` behave differently because of what you later pipe into grep. The `-a` option does include `.` and `..`, but piping it through grep changes what gets shown because grep reads filtered content.
I’m really trying to keep track. So `ls -a` shows them, but when it goes to grep, some entries might just disappear? That’s confusing.
It sounds like the shell might be expanding `.*` when you run `grep .*`. Instead of passing the regex you intend, it interprets `.*` as a list of files. At that point, grep isn't just filtering the output of `ls`; it's likely interpreting the files directly instead.
Thanks! I did a diff to check, and it looks like what you said is correct. Understanding grep behavior is tricky for me.

But won’t single quotes stop wildcard expansion completely? What if I want to use them?