Thursday, March 02, 2006

unix tip: wildcard - asterix (*) vs question mark (?)

ever wonder what is the different between asterix (*) and question mark (?) when you use them as wildcard?
here are some examples:

list all the files
$ ls
fileA fileAA fileAAA fileB fileBB fileBBB fileC fileCC fileCCC

if you use * it will listed all files
$ ls *
fileA fileAA fileAAA fileB fileBB fileBBB fileC fileCC fileCCC
Read more...

but if using ? it will not
$ ls ?
ls: ?: No such file or directory

list all files that start with file
$ ls file*
fileA fileAA fileAAA fileB fileBB fileBBB fileC fileCC fileCCC

but if you use 1 ? it will listed only files that start with file+1 character
$ ls file?
fileA fileB fileC

same goes when you using 2 or 3 ?
$ ls file??
fileAA fileBB fileCC
$ ls file???
fileAAA fileBBB fileCCC

conclusion:
the asterix (*) is to replace zero to multiple characters while the question mark (?) only to replace a single character & of course you can use them with other commands as well such as find, grep etc...

No comments: