0

I have question about regular expression.

There are two commands.

find / -regex '.*[^-_./0-9a-zA-Z].*'

find /

If I'm right, these two commands should output same results. Because, the first command has regular expression .* which appears at first. This means "Find any character and it can appear zero or more". This means all of the file name. Because file names consists of any character, including whitespace(" "),and it's not possible to appear zero character(It means file has no name). So, after all, I think they output same results.

But when I use these commands with | wc -l , which means "output the lines of results", the results are different.

Am I missing something?

2

Here is an example -- .* means 0 or more of any character. In the 2nd case it matches 0 of any character followed by one of the non excluded characters.

ls 
1  a  =a  ++a  a.0  a_1  B  b0

find .
.
./B
./=a
./b0
./a_1
./a
./1
./a.0
./++a

find . -regex '.*[^-_./0-9a-zA-Z].*'
./=a
./++a
2

.* is followed by [^-_./0-9a-zA-Z] - and it is not optional. So something other than one of those characters (the alphabets, digits, -, _, . and /) should appear once in the filename. Any path which consists solely of these characters will not be printed.

Therefore these commands need not output the same results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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