You are suffering from premature glob expansion.
.xa*
doesn't expand because it doesn't match anything in the current directory. (Globs are case sensitive.) However, .x*
does match some files, so this gets expanded by the shell before grep
ever sees it.
When grep
receives multiple arguments, it assumes the first is the pattern and the remainder are files to search for that pattern.
So, in the command ls -a | grep -i .x*
, the output of ls
is ignored, and the file ".xsession-errors.old" is searched for the pattern ".xsession-errors". Not surprisingly, nothing is found.
To prevent this, put your special characters within single or double quotes. For example:
ls -a | grep -i '.x*'
You are also suffering from regex vs. glob confusion.
You seem to be looking for files that start with the literal string ".x" and are followed by anything—but regular expressions don't work the same as file globs. The *
in regex means "the preceding character zero or more times," not "any sequence of characters" as it does in file globs. So what you probably want is:
ls -a | grep -i '^\.x'
This searches for files whose names start with the literal characters ".x", or ".X". Actually since there's only one letter you are specifying, you could just as easily use a character class rather than -i
:
ls -a | grep '^\.[xX]'
The point is that regular expressions are very different from file globs.
If you just try ls -a | grep -i '.x*'
, as has been suggested, you will be very surprised to see that EVERY file will be shown! (The same output as ls -a
directly, except placed on separate lines as in ls -a -1
.)
How come?
Well, in regex (but not in shell globs), a period (.
) means "any single character." And an asterisk (*
) means "zero or more of the preceding character." So that the regex .x*
means "any character, followed by zero or more instances of the character 'x'."
Of course, you are not allowed to have null file names, so every file name contains "a character followed by at least zero 'x's." :)
Summary:
To get the results you want, you need to understand two things:
- Unquoted special glob characters (including
*
, ?
, []
and some others) will get expanded by the shell before the command you are running ever sees them, and
- Regular expressions are different from (and more powerful than) file globs.
?
) in regular expressions means "zero or one of the preceding character"; it doesn't mean "any single character." There are two "main things." (As I mentioned earlier, in my answer.) ;) – Wildcard Oct 09 '16 at 12:05ls -a | grep -i '^\.x'
seems to be the correct way to search, in my case – Doe McBond Oct 09 '16 at 12:16ls
, don't do it, mkay? – cat Oct 09 '16 at 13:10