This command provides several operations on a file's name or attributes. Name is the name of a file; if it starts with a tilde, then tilde substitution is done before executing the command (see the manual entry for filename for details). Option indicates what to do with the file name. Any unique abbreviation for option is acceptable. The valid options are:
On Unix, -group gets or sets the group name for the file. A group id can be given to the command, but it returns a group name. -owner gets or sets the user name of the owner of the file. The command returns the owner name, but the numerical id can be passed when setting the owner. -permissions sets or retrieves the octal code that chmod(1) uses. This command does also has limited support for setting using the symbolic attributes for chmod(1), of the form [ugo]?[[+-=][rwxst],[...]], where multiple symbolic attributes can be separated by commas (example: u+s,go-rw add sticky bit for user, remove read and write permissions for group and other). A simplified ls style string, of the form rwxrwxrwx (must be 9 characters), is also supported (example: rwxr-xr-t is equivalent to 01755). On versions of Unix supporting file flags, -readonly gives the value or sets or clears the readonly attribute of the file, i.e. the user immutable flag uchg to chflags(1).
On Windows, -archive gives the value or sets or clears the archive attribute of the file. -hidden gives the value or sets or clears the hidden attribute of the file. -longname will expand each path element to its long version. This attribute cannot be set. -readonly gives the value or sets or clears the readonly attribute of the file. -shortname gives a string where every path element is replaced with its short (8.3) version of the name. This attribute cannot be set. -system gives or sets or clears the value of the system attribute of the file.
On Mac OS X and Darwin, -creator gives or sets the Finder creator type of the file. -hidden gives or sets or clears the hidden attribute of the file. -readonly gives or sets or clears the readonly attribute of the file. -rsrclength gives the length of the resource fork of the file, this attribute can only be set to the value 0, which results in the resource fork being stripped off the file.
file dirname c:/returns c:/.
Note that tilde substitution will only be performed if it is necessary to complete the command. For example,
file dirname ~/src/foo.creturns ~/src, whereas
file dirname ~returns /home (or something similar).
file join a b /foo barreturns /foo/bar.
Note that any of the names can contain separators, and that the result is always canonical for the current platform: / for Unix and Windows.
If 2 arguments are given, then these are assumed to be linkName and target. If linkName already exists, or if target does not exist, an error will be returned. Otherwise, Tcl creates a new link called linkName which points to the existing filesystem object at target (which is also the returned value), where the type of the link is platform-specific (on Unix a symbolic link will be the default). This is useful for the case where the user wishes to create a link in a cross-platform way, and does not care what type of link is created.
If the user wishes to make a link of a specific type only, (and signal an error if for some reason that is not possible), then the optional -linktype argument should be given. Accepted values for -linktype are “-symbolic” and “-hard”
On Unix, symbolic links can be made to relative paths, and those paths must be relative to the actual linkName's location (not to the cwd), but on all other platforms where relative links are not supported, target paths will always be converted to absolute, normalized form before the link is created (and therefore relative paths are interpreted as relative to the cwd). Furthermore, “~user” paths are always expanded to absolute form. When creating links on filesystems that either do not support any links, or do not support the specific type requested, an error message will be returned. In particular Windows 95, 98 and ME do not support any links at present, but most Unix platforms support both symbolic and hard links (the latter for files only) and Windows NT/2000/XP (on NTFS drives) support symbolic directory links and hard file links.
file split /foo/~bar/bazreturns / foo ./~bar baz to ensure that later commands that use the third component do not attempt to perform tilde substitution.
proc findMatchingCFiles {dir} { set files {} switch $::tcl_platform(platform) { windows { set ext .obj } unix { set ext .o } } foreach file [glob -nocomplain -directory $dir *.c] { set objectFile [file tail [file rootname $file]]$ext if {[file exists $objectFile]} { lappend files $file } } return $files }
Rename a file and leave a symbolic link pointing from the old location to the new place:
set oldName foobar.txt set newName foo/bar.txt # Make sure that where we're going to move to exists... if {![file isdirectory [file dirname $newName]]} { file mkdir [file dirname $newName] } file rename $oldName $newName file link -symbolic $oldName $newName
On Windows, a file can be “started” easily enough (equivalent to double-clicking on it in the Explorer interface) but the name passed to the operating system must be in native format:
exec {*}[auto_execok start] {} [file nativename ~/example.txt]