glob
and file
. glob
provides the access to the
names of files in a directory. It uses a name matching mechanism
similar to the UNIX ls command or the Windows (DOS)
dir command, to return a list of names that match a
pattern.
file
provides three sets of
functionality:
dirname
........ Returns directory
portion of pathextension
........ Returns file name
extensionjoin
........ Join directories and the
file name to one stringnativename
....... Returns the
native name of the file/directoryrootname
....... Returns file name
without extensionsplit
........ Split the string into
directory and file namestail
.................... Returns
filename without directoryatime
................ Returns time of
last accessexecutable
..... Returns 1 if file is
executable by userexists
................ Returns 1 if
file existsisdirectory
...... Returns 1 if entry
is a directoryisfile
.................. Returns 1 if
entry is a regular filelstat
................... Returns
array of file status informationmtime
............... Returns time of
last data modificationowned
................ Returns 1 if
file is owned by userreadable
............ Returns 1 if
file is readable by userreadlink
............. Returns name of
file pointed to by a symbolic linksize
..................... Returns
file size in bytesstat
..................... Returns
array of file status informationtype
.................... Returns type
of filewritable
............ Returns 1 if
file is writeable by usercopy
................ Copy a file or a
directorydelete
................ Delete a file
or a directorymkdir
................ Create a new
directoryrename
................ Rename or move
a file or directoryBetween these two commands, a program can obtain most of the information that it may need and manipulate the files and directories.
While retrieving information about what files are present and what properties they have is usually a highly platform-dependent matter, Tcl provides an interface that hides almost all details that are specific to the platform (but are irrelevant to the programmer).
To take advantage of this feature, always manipulate file names
via the file
join
, file
split
commands and the others in the first
category.
For instance to refer to a file in a directory upwards of the current one:
set upfile [file join ".." "myfile.out"] # upfile will have the value "../myfile.out"
(The ".." indicates the "parent directory")
Because external commands may not always deal gracefully with the uniform representation that Tcl employs (with forward slashes as directory separators), Tcl also provides a command to turn the string into one that is native to the platform:# # On Windows the name becomes "..\myfile.out" # set newname [file nativename [file join ".." "myfile.out"]]
Retrieving all the files with extension ".tcl" in the current directory:
set tclfiles [glob *.tcl] puts "Name - date of last modification" foreach f $tclfiles { puts "$f - [clock format [file mtime $f] -format %x]" }(The clock command turns the number of seconds returned by the
file
mtime
command into a simple date string, like "12/22/04")
glob
?switches?
pattern
?patternN?
pattern
or patternN
switches
may be one of the following
(there are more switches available):
-nocomplain
glob
to return an empty
list without causing an error. Without this flag, an error would be
generated when the empty list was returned.-types
typeList
typeList
may consist of type
letters, like a "d" for directories and "f" for ordinary files as
well as letters and keywords indicating the user's permissions ("r"
for files/directories that can be read for instance).--
pattern
follows the same matching rules
as the string match globbing rules with these exceptions:
pattern
are ~/, then the ~ is replaced by the value of the
HOME environment variable.pattern
is a
~, followed by a login id, then the ~loginid is
replaced by the path of loginid's home directory.Note that the filenames that match pattern
are returned in an arbitrary order (that is,
do not expect them to be sorted in alphabetical order, for
instance).
file
atime
name
name
was last accessed. Generates an error
if the file doesn't exist, or the access time cannot be
queried.file
copy
?-force?
name
target
name
to a new
file target
(or to an existing directory
with that name)-force
allows you to overwrite
existing files.file
delete
?-force?
name
name
.-force
allows you to delete
non-empty directories.file
dirname
name
name
contains no slashes, file
dirname
returns a
".". If the last "/" in name
is also the
first character, it returns a "/".file
executable
name
name
is executable
by the current user, otherwise returns 0.file
exists
name
name
exists, and
the user has search access in all the directories leading to the
file. Otherwise, 0 is returned.file
extension
name
file
isdirectory
name
file
isfile
name
file
lstat
name
varName
varName
. The indexes in varName
are:
atime
.......time of last accessctime
.......time of last file status
changedev
...........inode's devicegid
............group ID of the file's
groupino
............inode's numbermode
.......inode protection modemtime
.....time of last data
modificationnlink
........number of hard linkssize
...........file size, in
bytestype
..........Type of Fileuid
.............user ID of the file's
ownername
is a symbolic link, the values in varName
will refer to the link, not the file that is linked to. (See also
the stat
subcommand)file
mkdir
name
name
.file
mtime
name
file
owned
name
file
readable
name
file
readlink
name
name
isn't a symlink, or can't be read, an error
is generated.file
rename
?-force?
name
target
name
to the new
name target
(or to an existing directory
with that name)-force
allows you to overwrite
existing files.file
rootname
name
name
up
to but not including the last ".". Returns $name
if name
doesn't include
a ".".file
size
name
name
in
bytes.file
stat
name
varName
varName
. The indexes in varName
are:
atime
.......time of last accessctime
.......time of last file status
changedev
...........inode's devicegid
............group ID of the file's
groupino
............inode's numbermode
.......inode protection modemtime
.....time of last data
modificationnlink
........number of hard linkssize
...........file size in bytestype
..........Type of fileuid
.............user ID of the file's
ownerfile
tail
name
name
after the last slash. Returns the name if name
contains no slashes.file
type
name
file
...................................Normal
filedirectory
........................DirectorycharacterSpecial
.......Character
oriented deviceblockSpecial
.............. Block
oriented devicefifo
...................................Named pipelink
..................................Symbolic
linksocket
...........................Named
socketfile
writable
name
Note: The overview given above does not cover all the details of the various subcommands, nor does it list all subcommands. Please check the man pages for these.
# # Report all the files and subdirectories in the current directory # For files: show the size # For directories: show that they _are_ directories # set dirs [glob -nocomplain -type d *] if { $dirs != {} } { puts "Directories: foreach d [lsort $dirs] { puts " $d" } } else { puts "(no subdirectories)" } set files [glob -nocomplain -type f *] if { $files != {} } { puts "Files: foreach f [lsort $files] { puts " [file size $f] - $f" } } else { puts "(no files)" }