Let's consider a directory with two entries: a file and a directory. It may look like:
$ ls -lThe ls command above was executed inside our directory, without arguments, hence it listed the current directory's contents. However, if we pass a wildcard we get more results than expected:
total 12K
drwxr-xr-x 2 jmmv jmmv 4096 Dec 19 15:18 foodir
-rw-r--r-- 1 jmmv jmmv 0 Dec 19 15:18 foofile
$ ls -l *What happened in the previous command is that the shell expanded the wildcard; that is, ls never saw the special character itself. In fact, the above was internally converted to ls -l foofile foodir and this is what was actually passed to the ls utility during its execution. With this in mind, it is easy to see why you got the contents of the sample directory too: you explicitly (although somewhat "hidden") asked ls to show them.
-rw-r--r-- 1 jmmv jmmv 0 Dec 19 15:18 foofile
foodir:
total 4K
-rw-r--r-- 1 jmmv jmmv 0 Dec 19 15:19 anotherfile
How to avoid that? Use ls's -d option, which tells it to list the directory entries themselves, not their contents:
$ ls -l -d *Update (21st Dec): Fixed the first command shown as noted by Hubert Feyrer.
drwxr-xr-x 2 jmmv jmmv 4096 Dec 19 15:19 foodir
-rw-r--r-- 1 jmmv jmmv 0 Dec 19 15:18 foofile
3 comments:
If i did add -d from the start as you said, it'd probably DTRT right away. Chances are that I forgot, so maybe you should expect the first try to be "ls f*", not "ls -d f*". :)
Nice point about how shell cards work, otherwise!
- Hubert
linux default shell/locale/glob behaviour globs [a-z] over A-Z as well as a-z. -so for i in [a-z]* can have unexpected consequences, if you are used to a BSD default collating behaviour where they are different.
Hubert: thanks! I made that exact mistake in the title and fixed it soon after posting, but didn't note the one in the text. It's fixed now.
Anonymous: I know; this is why my 'ls' alias looks like "alias ls='LC_ALL=C /bin/ls -F'" on all systems. (OK, this does not affect all commands.)
Post a Comment