glob(3tcl)
π glob β NAME π
okay bestie, listen up: glob returns the names of files that match patterns. that's it, that's the whole vibe β¨
π¦ SYNOPSIS π¦
glob ?switches? ?pattern ...?
πΈ DESCRIPTION πΈ
so this command does file name "globbing" β and yes it's basically the same energy as the csh shell or the bash shell, no cap. It hands you back a list of the files whose names match any of the pattern arguments you gave it.
π¨ important: no particular order is guaranteed in that list. like, none. so if you want it sorted, bestie, you gotta call lsort yourself. periodt.
β¨ OPTIONS β¨
If the initial arguments to glob start with -, then they get treated as switches. here's the whole lineup, no skipping π«Ά:
-directory directoryβ search for files matching the given patterns, but starting inside the given directory. this is the move when the directory name is full of glob-sensitive characters and you do NOT want to sit there quoting every single one. π heads up: you may not use this together with-pathβ-pathis the one that lets you search complete file paths whose names might contain glob-sensitive characters.-joinβ the remaining pattern arguments (after option processing is done) get treated as a single pattern, made by joining the arguments together with directory separators. it's giving "stop making me type slashes" π-nocomplainβ lets an empty list be returned without an error. lowkey obsolete though: this is the default behavior in Tcl 9.0, so this switch doesn't do anything anymore. it's just here vibing.-path pathPrefixβ search for files with the given pathPrefix, where the rest of the name matches the given patterns. this lets you search for files with names similar to a given file (as opposed to a directory!) even when those names contain glob-sensitive characters. can't be combined with-directoryβ nope, not allowed. for example, to find all files with the same root name as$pathbut different extensions, you should useglob -path [file rootname $path] .*β and that will work even if$pathis absolutely crammed with glob-sensitive characters. iconic.-tailsβ only return the part of each file found which follows the last directory named in any-directoryor-pathpath specification. soglob -tails -directory $dir *is equivalent toset pwd [pwd]; cd $dir; glob *; cd $pwd. and for-pathspecifications, the returned names will include the last path segment, soglob -tails -path [file rootname /home/fred/foo.tex] .*will return paths likefoo.auxfoo.bibfoo.texetc. π-types typeListβ only list files or directories that match typeList. the items in that list come in two forms, and this is the part where I need you to focus bestie:
the first form is like the -type option of the Unix find command: b (block special file), c (character special file), d (directory), f (plain file), l (symbolic link), p (named pipe), or s (socket). you can specify multiple types in the list. glob will return all files which match at least one of the types given.
π note: symbolic links will be returned both if-types lis given, or if the target of a link matches the requested type. so a link to a directory will be returned if-types dwas specified. sneaky but true π
the second form specifies types where all the types given must match. these are r, w, x as file permissions, and readonly, hidden as special permission cases. on the Macintosh, macOS types and creators are also supported β any item which is four characters long is assumed to be a macOS type (e.g. TEXT). items of the form {macintosh type XXXX} or {macintosh creator XXXX} will match types or creators respectively. unrecognized types, or specifications of multiple macOS types/creators, will signal an error. π¨
the two forms may be mixed, so -types {d f r w} will find all regular files OR directories that have both read AND write permissions. and these two are equivalent:
glob -type d *
glob */
except that the first case doesn't return the trailing / and is more platform independent. β¨
--β marks the end of switches. the argument following this one will be treated as a pattern even if it starts with a-. π
π GLOBBING PATTERNS π
the pattern arguments may contain any of these special characters β and this set is a superset of the ones supported by string match:
?β matches any single character.*β matches any sequence of zero or more characters.[chars]β matches any single character in chars. if chars contains a sequence of the forma-bthen any character betweenaandb(inclusive) will match.\xβ matches the character x.{a,b,...}β matches any of the sub-patternsa,b, etc.
On Unix, same as with csh, a . at the beginning of a file's name β or just after a / β must be matched explicitly or with a {} construct, unless the -types hidden flag is given (because a . at the start of a file's name means it's hidden). on other platforms, files beginning with a . are handled no differently to any others, except the special directories . and .., which must be matched explicitly β and that's on purpose, to stop a recursive pattern like glob -join * * * * from recursing up the directory hierarchy as well as down. π§ plus: all / characters must be matched explicitly.
the glob command differs from csh globbing in two ways, bestie:
- it does not sort its result list (use the
lsortcommand if you want that list sorted). globonly returns the names of files that actually exist β in csh, no check for existence is made unless a pattern contains a?,*, or[]construct.
πͺ WINDOWS PORTABILITY ISSUES πͺ
for Windows UNC names, the servername and sharename components of the path may not contain ?, *, or [] constructs. π
and since the backslash character has a special meaning to the glob command, glob patterns containing Windows style path separators need special care. the pattern C:\\foo\\* is interpreted as C:\foo\*, where \f will match the single character f and \* will match the single character * and will not be interpreted as a wildcard character. one solution to this problem is to use the Unix style forward slash as a path separator. Windows style paths can be converted to Unix style paths with the command file join $path or file normalize $path. π
π EXAMPLES π
find all the Tcl files in the current directory:
glob *.tcl
find all the Tcl files in the user's home directory, no matter what the current directory is:
glob -directory [file home] *.tcl
find all subdirectories of the current directory:
glob -type d *
find all files whose name contains an a, a b, or the sequence cde:
glob -type f *{a,b,cde}*
ate. no crumbs. π
π SEE ALSO π
π KEYWORDS π
exist, file, glob, pattern
okay that's the whole page bestie, you're officially a glob girlie now π«Άβ¨ this was the glob(3tcl) page from the Tcl Built-In Commands manual β Tcl 8.3, if you're keeping track, and yes I always am π ππ
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms