Tcl glob Command
last modified October 20, 2026
The Tcl glob command is used to match file names against patterns.
It returns a list of files that match specified patterns. This is useful for
file operations and directory listings.
Basic Definition
The glob command expands wildcard patterns to match file names.
It supports various pattern matching characters similar to Unix shell globbing.
Syntax: glob ?switches? pattern ?pattern ...?. The command returns
a list of files matching the pattern(s); at least one pattern is required. The
common switches are -nocomplain, -directory,
-types, -tails, -join and
-- (end of switches).
Basic File Matching
This example shows the simplest usage of glob to list all .tcl
files in the current directory.
set tcl_files [glob *.tcl] puts "Tcl files: $tcl_files"
The pattern *.tcl matches all files ending with .tcl. The result
is stored in tcl_files and printed. On Linux the match is
case-sensitive; Windows file systems are case-insensitive. If no file matches,
glob raises an error unless -nocomplain is used.
Multiple Patterns
glob accepts any number of patterns as separate arguments.
set files [glob *.tcl *.txt] puts "Matching files: $files"
This matches both .tcl and .txt files. The result is a combined list of all files matching either pattern. The order of the result isn't guaranteed, and an error is raised only if none of the patterns match anything.
Hidden Files
By default, a wildcard in glob does not match a leading dot, so
hidden files are not returned by *; the dot must be written in the
pattern.
set hidden_files [glob -nocomplain .*] puts "Hidden files: $hidden_files"
The .* pattern matches hidden files, and also the special
entries . and .., which is why the output shows
them. The -nocomplain switch returns an empty list if no matches
exist instead of raising an error. This directory contains no other hidden
files.
Directory Recursion
The glob command does not descend into subdirectories by itself.
A recursive search combines the -directory and -types
switches with a procedure that calls itself.
proc find_files {dir pattern} {
set files [glob -nocomplain -directory $dir -types f $pattern]
foreach sub [glob -nocomplain -directory $dir -types d *] {
lappend files {*}[find_files $sub $pattern]
}
return $files
}
puts "All Tcl files: [find_files . *.tcl]"
This finds all .tcl files in the current directory and its subdirectories. The
procedure collects the matching files of a directory and then calls itself for
each subdirectory. Hidden subdirectories are skipped, because the pattern
* does not match a leading dot.
Type Filtering
The -types switch filters matches by file type (file, directory, etc).
set directories [glob -nocomplain -types d *] puts "Directories: $directories"
This lists only directories in the current folder; the output is empty here
because the scratch folder has none. -types d matches
directories, while -types f would match only plain files.
Case Insensitive Matching
The glob command has no case-insensitive switch, but character
classes in the pattern can match both cases.
set image_files [glob -nocomplain {*.[jJ][pP][gG]} {*.[pP][nN][gG]}]
puts "Image files: $image_files"
This matches .jpg/.JPG and .png/.PNG files regardless of case. The patterns are in braces so that Tcl does not treat the brackets as command substitution. Useful when dealing with files from different operating systems.
Best Practices
- Error Handling: Use
-nocomplainwhen matches might not exist. - Quoting: Put patterns with brackets in braces {} so that Tcl does not run them as commands.
- Performance: Be cautious with recursive globs on large directories.
- Platform: Remember that case sensitivity depends on the file system.
- Sorting: Sort results if order matters in your application.
This tutorial covered the Tcl glob command with practical
examples showing its usage in different file matching scenarios.
Author
List all Tcl Tutorials.