3

I'm running Ubuntu as VM in VMWare Fusion on MacPro:

ayakovlev@ubuntu:~$ uname -a
Linux ubuntu 3.11.10.3 #1 SMP Thu Mar 6 15:31:54 EST 2014 x86_64 x86_64 x86_64 GNU/Linux

The command below outputs result which I expect:

ayakovlev@ubuntu:~$ ls /usr/include/*5*
ax25.h

When I replace 5 by 6 I get very strange results:

ayakovlev@ubuntu:~$ ls /usr/include/*6*
a.out.h  asm  bits  c++  fpu_control.h  gnu  ieee754.h  sys  zconf.h

What might be a reason for that?

strugee
  • 1,092
HEKTO
  • 555
  • 1
  • 8
  • 24

2 Answers2

6

The wildcards in ls /usr/include/*6* expand to whatever matches. In the first case, it matched specific regular files. However, if it matches directories such as /usr/include/x86_64-linux-gnu, it'll print out the contents of those directories instead (i.e. it would have expanded to ls /usr/include/x86_64-linux-gnu).

You can verify that the wildcard would have expanded to a directory with ls /usr/include | grep 6.

Sparhawk
  • 6,929
  • 2
    To list directories entries instead of its contents you can use -d: ls -d /usr/include/*6*. – Eric Carvalho Mar 24 '14 at 01:32
  • In all other Unixes I worked the ls used to show a directory before list of files from this directory. Including the Mac OS X, where I can see a bunch of directories, containing the digit 6, followed by files from these directories. – HEKTO Mar 24 '14 at 02:06
  • @AlekseyYakovlev I see the same thing in Ubuntu, but only if there are multiple matches for the wildcard. If the wildcard only matches one item, and it is a directory, it won't list the directory name before listing the contents. – Sparhawk Mar 24 '14 at 02:27
2

As @Sparhawk explained, what is happening is that you are listing the content of the directories. This will not depend on the *nix you are using, as far as I know, this is the default behavior of GNU ls. Non-GNU implementations may be different but I doubt it.

When you run this command:

ls /usr/include/*6*

The glob (*6*) is expanded by your shell (bash for example) before calling ls. So, given the following directory structure:

.
├── dir1
│   ├── file1
│   └── file2
└── dir2
    ├── file1
    └── file2

ls d* will be expanded to ls dir1 dir2, and give the results you expect:

dir1:
file1  file2

dir2:
file1  file2

This is how ls deals with multiple target directories by default, it lists each directory found and then the contents of that directory. However, if you run it on a single directory, the name is not printed:

$ ls d*1
file1  file2

So, since there is only one match for the glob *6* in /usr/include and that match is a directory, you are running ls with a single directory as input and, therefore, it is listing the contents of the directory without including the directory name.

terdon
  • 100,812