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.

I am a beginner terminal user trying to understand wildcards. I think I pretty much have a good grasp on it, but I have a couple of questions:

Why do the following two codes work as expected:

ls -d .[!.]?*
echo [D]*

But the following 2 don't:

ls -d [.][!.]?*
echo [D]

In the first command, I get an error:

ls: cannot access [.][!.]*: No such file or directory

When I am trying to get the same listing ls -d .[!.]?*. And for the second, the output is:

[D]

When I'm expecting an error proclamation along the lines of No such file or directory. What am I missing? What exactly makes an expression a wildcard, if those wildcard elements shown in the second set of examples above don't cut it?

Clarification (Also in comments):

The wildcard [D]* doesn't only output D, it also outputs Desktop, Downloads ... etc. However, I also tried echo [D] when I had a file named D and when I didn't. The output worked when the file D was there, but I also got the output [D] when it wasn't. I don't get why. Why did the presence of the file in the directory change the expression [D] from being a wild card to not?

share|improve this question
    
You have a file named D in that directory? –  depquid Apr 2 '13 at 21:57
    
Yes, for practice :D –  naxchange Apr 2 '13 at 22:54
    
But not when you got the output that you put in your question. You shouldn't say, "When I'm expecting the file named D," when no file by the name exists at the time. –  depquid Apr 3 '13 at 0:32
    
First of all, I really appreciate that you took the time to provide help that you gave, and I apologize for the poor wording of my original question. But, what I actually implied with the "I am expecting the file named D" was an error proclamation along the lines of No such file or directory. –  naxchange Apr 3 '13 at 1:25
    
The way your question read implied that you thought you had a file named D (especially when added a comment asserting you did) when you got behavior indicating you didn't (because you actually didn't at the time). You need to make sure that the conditions you describe and the output you list correspond to a single state of the system. If the conditions change, you need to provide an update explaining both the new conditions and new output. –  depquid Apr 3 '13 at 12:27

1 Answer 1

up vote 4 down vote accepted

By default, bash (the shell) doesn't expand a filename beginning with a dot (considered a hidden file) unless the dot is explicitly specified. Since [.] is a pattern, rather than a literal character, it doesn't trigger the display of hidden files and so ls -d [.][!.]?* doesn't match anything.

You can change this default behavior by running shopt -s dotglob, which sets the option to always includes hidden files when expanding filename patterns (known as "globbing").

Example (the dollar sign at the beginning of the line indicates the shell prompt; don't type it in):

$ touch .foo
$ ls -d .[!.]?*
.foo
$ ls -d [.][!.]?*
ls: cannot access [.][!.]?*: No such file or directory
$ shopt -s dotglob
$ ls -d [.][!.]?*
.foo

The issue with echo [D] is that expansion doesn't happen if no filename is matched. Since you didn't have a file named D when you tried it, echo was given, and echoed, what you literally entered.

An example of pathname expansion only occurring when a filename matches:

$ touch apple
$ echo a*
apple
$ rm apple
$ echo a*
a*

If you delete all of the files which match [D]*, then echo [D]* will give you [D]*.

share|improve this answer
    
Ok, that clears things up. What about the second example? Any ideas? –  naxchange Apr 2 '13 at 22:56
1  
@naxchange Are you absolutely sure that you have a file called D? If echo [D] prints [D] but echo [D]* seems to print D, that indicates that you have a file whose name is D and one or more whitespace or unprintable character. –  Gilles Apr 2 '13 at 23:37
    
The wildcard [D]* doesn't only output D, it also outputs Desktop, Downloads ... etc. However, I also tried echo [D] when I had a file named D and when I didn't. The output worked when D was there, but I also got the output [D] when it wasn't. I don't get why. Why did the presence of the file in the directory change the expression [D] from being a wild card to not? –  naxchange Apr 3 '13 at 0:05
    
@naxchange You've changed from what you put in your question. Please don't do that in the comments; edit your question if necessary. Anyway, I've updated my answer accordingly. –  depquid Apr 3 '13 at 0:27

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.