glob(3tcl)

πŸ“¦
version 9.0.4 Β· 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– 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 β€” -path is 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 $path but different extensions, you should use glob -path [file rootname $path] .* β€” and that will work even if $path is absolutely crammed with glob-sensitive characters. iconic.
  • -tails β€” only return the part of each file found which follows the last directory named in any -directory or -path path specification. so glob -tails -directory $dir * is equivalent to set pwd [pwd]; cd $dir; glob *; cd $pwd. and for -path specifications, the returned names will include the last path segment, so glob -tails -path [file rootname /home/fred/foo.tex] .* will return paths like foo.aux foo.bib foo.tex etc. πŸ“
  • -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 l is given, or if the target of a link matches the requested type. so a link to a directory will be returned if -types d was 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 form a-b then any character between a and b (inclusive) will match.
  • \x β€” matches the character x.
  • {a,b,...} β€” matches any of the sub-patterns a, 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:

  1. it does not sort its result list (use the lsort command if you want that list sorted).
  2. glob only 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