glob [[switches]] pattern [[pattern ...]] Supported ''switches'' are: http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/glob.htm The [[glob]] command lists all the directory entries (files, directories, and links) whose names match a given pattern. It's usually best to build up the pattern with [[file join]] (see the [file] command for details). To list all the entries in a given directory, for instance, use ** Description ** [lsort [glob [file join $dir {{.*,*}}]]] '''`glob`''' lists all the directory entries whose names match a given Note that file names beginning with a period are hidden by default. The pattern above expands these hidden files as well as the ordinary ones. ======none However, if $dir contains characters which are glob-sensitive (any of those square/curly brackets, stars, question marks etc), the above example will fail. Therefore in Tcl 8.3 or newer, you should always use: [lsort [glob -dir $dir {.*,*}]] which actually has the benefit of being clearer to read too! If you want to use this construction in older versions of Tcl, see [glob forwards compatibility] for a way to override the older [[glob]] command. ====== Indicate the end of the switch by "--" if your pattern contains "-". [LV] Actually, you should ALWAYS use the ''--'' ; there is no problem using it, and unless you are hardcoding literal arguments to glob you may accidentally get an argument beginning with a dash. To get a list of all contents of a directory that aren't themselves ---- The [[glob]] command can be used to simulate [ls -l in Tcl]. ---- In more recent versions of Tcl, glob even has a flag to specify whether one wants just directories or files returned. ---- '''Stale links:''' ''glob'' has a funny behavior (seen on Solaris 5.5.1) with symbolic links that don't point to an existing file: % exec ln -s /does/not/exist stalelink % glob stalelink no files matched glob pattern "stalelink" % glob stalelink* stalelink If the pattern is not wildcarded, it tries to follow the link; otherwise it just returns the link's name itself. Hmm... ([RS]) ====== ---- [Tcl warts] One might consider at least one of glob's behaviors as a wart. glob uses the brace ({}) characters as special notation, even though the tcl parser ALSO uses these characters. The result is that one needs to learn, from the beginning, that one needs to quote these characters when using them in glob arguments. '''EE:''' Actually, I'm of the opinion that one should always "quote" or {brace} arbitrary strings that are being used as command arguments. [LV] I agree. However, not everyone does so. It would look weird to say things like string "length" $a or even somecmd "-length" "60" , wouldn't it? [PYK] 2014-08-13: edited the example above to use `[[[file tail] $item]]` '''EE:''' Yes it would, and I hadn't thought of that. But I draw a distinction between the parts of the command itself, and the rest, which are the arguments. For your examples, '''[string] length''' is really the command, and '''somecmd -length''' is really the command, and we generally don't quote the command. But I'll usually do '''string length "$a"''', or '''somecmd -length "60"'''. And I'll ''certainly'' do '''string compare "$stringone" "$string2"''', because it once took me two days to track down a bug caused by failing to put the arguments to a string compare into quotes. [RS] To the parser, the quotes just mean "group me" which is important if the quotes contain whitespace. If no substitutions are wanted, curly braces have the same effect, so "a rose" == {a rose} != a rose For strings without whitespace, quotes are redundant, but may still serve a documentation purpose for the reader (e.g. in [Salt and Sugar], sugar arguments are quoted to express that they are just sugar). (Note that these are mutually exclusive. Also note that `-directory` can be '''EE:''' Is that completely true? The bug I speak of above was in a case where I was doing string manipulation on a variable which contained 32 characters of purely digits, and it was erroring until I put the $variable into quotes. Let's say I want to find all files in a directory, then I use (ignoring hidden '''[LV]''' Likewise, the point I was trying to make above was that for glob (the subject of this page), one needs to do particularly special quoting to ensure that the parser doesn't touch the { and } characters, but lets them pass on to glob itself... [Tcl syntax help] - [Arts and crafts of Tcl-Tk programming]