CMD: *.* or just *?

File name and extension have been a single field ever since Windows 95 and NT 3.5 introduced "long file name" support, and wildcard matches are done against the entire filename at once. As a result, you can have a filename with no dots in it (perhaps rare for files, but very common for folders/directories) and at first glance *.* wouldn't actually match such files.

Old scripts using *.* will still work because of compatibility code – if the wildcard ends with .*, that part is ignored by the OS. (So if you wanted to specifically match files with an extension, I guess you would need *.?* for that.)

But it's not something you should rely on; if you're writing scripts for modern Windows versions, follow their conventions, not MS-DOS conventions. (Note that as of Windows NT, .bat scripts are not interpreted by MS-DOS anymore but by cmd.exe, a native Win32 program.)


On Linux and various other Unixen, name & extension have never been separate in the first place, and there isn't any special magic to make *.* work, so * is the only choice that makes sense.


It's probably worth mentioning that the unixy/posixy shells like bourne shell, bash, ksh, zsh, etc do wildcard expansion (of glob characters like *, ?, [range], [!range] and other expansions like braces and extended globs) to compile a list of arguments before the command is executed. So this expansion is done by the shell not the command to which these may be arguments of.

i.e. The shell is responsible for what *, *.* expands to

 $ ls
 file.csv  file.doc  file.pdf  file.txt  file.xlsx  zz-file-without-extension

 $ (set -xv; foo *)   # is actually expanded to the following
   + foo file.csv file.doc file.pdf file.txt file.xlsx zz-file-without-extension

 $ (set -xv; foo *.*)  # note this does not match `zz-file-without-extension`
   + foo file.csv file.doc file.pdf file.txt file.xlsx

This is not the case in CMD (and similarly for powershell utilities) as it passes the glob characters verbatim in to the executed command - and so expansion is the responsibility of the command/utility and not the shell. So ultimately, what *.* or * means is left to the utility leaving it to conform (or not) to conventions - which is why CMD's utilities like dir *.* also matched (arguably incorrectly yet preserving expectations) files without extensions.

I believe it's safe to summarize this way.

  • Under CMD it depends on the utility.
  • Under PowerShell, utilities that make use of the WildCardPattern Class will provide a consistent subset of posixy behaviours.

Tags:

Command Line