[ Team LiB ] Previous Section Next Section

Manipulating Files and Directories

Tcl 7.6 added file operations to copy files, delete files, rename files, and create directories. In earlier versions it was necessary to exec other programs to do these things, except on Macintosh, where cp, rm, mv, mkdir, and rmdir were built in. These commands are no longer supported on the Macintosh. Your scripts should use the file command operations described below to manipulate files in a platform-independent way.

File name patterns are not directly supported by the file operations. Instead, you can use the glob command described on page 122 to get a list of file names that match a pattern.

Copying Files

The file copy operation copies files and directories. The following example copies file1 to file2. If file2 already exists, the operation raises an error unless the -force option is specified:

file copy ?-force? file1 file2

Several files can be copied into a destination directory. The names of the source files are preserved. The -force option indicates that files under directory can be replaced:

file copy ?-force? file1 file2 ... directory

Directories can be recursively copied. The -force option indicates that files under dir2 can be replaced:

file copy ?-force? dir1 dir2

Creating Directories

The file mkdir operation creates one or more directories:

file mkdir dir dir ...

It is not an error if the directory already exists. Furthermore, intermediate directories are created if needed. This means that you can always make sure a directory exists with a single mkdir operation. Suppose /tmp has no subdirectories at all. The following command creates /tmp/sub1 and /tmp/sub1/sub2:

file mkdir /tmp/sub1/sub2

The -force option is not understood by file mkdir, so the following command accidentally creates a folder named -force, as well as one named oops.

file mkdir -force oops

Symbolic and Hard Links

The file link operation allows the user to manipulate links. Hard links are directory entries that directly reference an existing file or directory. Symbolic (i.e., soft) links are files that contain the name of another file or directory. Generally, opening a link opens the file referenced by the link. Operating system support for links varies. Unix supports both types of links. Classic Macintosh only supports symbolic links (i.e., aliases). Windows 95/98/ME do not support links at all, while Windows NT/2000/XP support symbolic links to directories and hard links to files.

With only a single argument, file link returns the value of a symbolic link, or raises an error if the file is not a symbolic link. With two pathname arguments, the first is the name of the link, and the second is the name of the file referenced by the link. If you leave out the -hard or -symbolic, the appropriate link type is created for the current platform:

file link the_link the_existing_file

Deleting Files

The file delete operation deletes files and directories. It is not an error if the files do not exist. A non-empty directory is not deleted unless the -force option is specified, in which case it is recursively deleted:

file delete ?-force? name name ...

To delete a file or directory named -force, you must specify a nonexistent file before the -force to prevent it from being interpreted as a flag (-force -force won't work):

file delete xyzzy -force

Renaming Files and Directories

The file rename operation changes a file's name from old to new. The -force option causes new to be replaced if it already exists.

file rename ?-force? old new

Using file rename is the best way to update an existing file. First, generate the new version of the file in a temporary file. Then, use file rename to replace the old version with the new version. This ensures that any other programs that access the file will not see the new version until it is complete.

    [ Team LiB ] Previous Section Next Section