Reputation: 103
I'm reading the book "How Linux Works". The author gave the following regular expressions so I can get all dot files except the current and parent directories. (Page 21)
.??*
.[^.]*
If any dot files exist in the directory the both work. But when no dot files exist only the first one work.
I can't understand them. Can you describe me?
$ ls -a
. .. .hiding something
$ ls -a | grep .??*
.hiding
$ ls -a | grep .[^.]*
.hiding
$ mv .hiding hiding
$ ls -a | grep .??*
$ ls -a | grep .[^.]*
.
..
hiding
something
Upvotes: 2
Views: 278
Reputation: 2314
The first does not really make sense, does not work for me, and I cannot find any documentation about ??
either.
Regardless, there are two problems with both of these regexes:
The .
here matches any char. In order to match only for a single dot as it is, you have to put a \
in front of it, like \.
.
The whole expression can match anywhere in the line. You have to assert that the matching starts at the beginning. So start with ^
.
Try: ls -a | grep '^\.[^.]'
This means: starting at the beginning of the line, find a single dot. Then any char that is not listed (negation is done by the second ^
here) between the brackets, so not a literal dot.
In brackets you don't have to use \
, although you can.
Upvotes: 2