:TITLE: Changing Working Directory - cd, pwd ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl35.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# ::CMD:: if {([info exists tcl_platform])} { switch $tcl_platform(platform) { unix { set Tutor(lsn.codeMod) { {regsub TEMPDIR $line "/bin /usr/bin /usr/local/bin]" line} } } windows { set Tutor(lsn.codeMod) { {regsub TEMPDIR $line "C:/windows C:/windows/system C:/temp C:/foo " line} } } mac { set Tutor(lsn.codeMod) { {regsub TCL_INTERP $line "$tutorGlobals(bindir):$tutorGlobals(interpreter)" line} {regsub "wish" $line "tclsh" line} } } default { puts "I don't recognize the platform: $tcl_platform(platform)" puts "Can't set platform specific parameters" } } } :LESSON_TEXT_START_LEVEL 0: Tcl also supports commands to change and display the current working directory.

These are:

cd ?dirName?
Changes the current directory to dirName (if dirName is given, or to the $HOME directory if dirName is not given. If dirname is a tilde (~, cd changes the working directory to the users home directory. If dirName starts with a tilde, then the rest of the characters are treated as a login id, and cd changes the working directory to that user's $HOME.
pwd
Returns the current directory.
:TEXT_END: :LESSON_TEXT_START_LEVEL 1: Tcl also supports commands to change and display the current working directory.

These are:

cd ?dirName?
Changes the current directory to dirName (if dirName is given, or to the $HOME directory if dirName is not given. If dirname is a tilde (~, cd changes the working directory to the users home directory. If dirName starts with a tilde, then the rest of the characters are treated as a login id, and cd changes the working directory to that user's $HOME.
pwd
Returns the current directory.
:TEXT_END: :LESSON_TEXT_START_LEVEL 2: Most modern operating systems support a tree based directory structure, one in which a given directory may have numerous subdirectories, which may also have subdirectories.

The path to a given file or subdirectory can be arbitrarily long, and can become difficult to work with. Thus, it is convenient to change the current working directory to one that contains the files the program is accessing.

Tcl supports commands to change and display the current working directory.

These are:

cd ?dirName?
Changes the current directory to dirName (if dirName is given, or to the $HOME directory if dirName is not given. If dirname is a tilde (~, cd changes the working directory to the users home directory. If dirName starts with a tilde, then the rest of the characters are treated as a login id, and cd changes the working directory to that user's $HOME.
pwd
Returns the current directory.

The example code could also be written as:

foreach dir $dirs {
  set c_files [glob $dir/c*]
  
  foreach name $c_files {
    puts "[format "%-15s  %-20s " [file tail $name] $dir ]"
    }
  }
Adding the directory name to the glob command also adds that directory path to each name in the list. Depending on your program you may want or not want that information carried with each name. :TEXT_END: :CODE_START: set dirs [list TEMPDIR] puts "[format "%-15s %-20s " "FILE" "DIRECTORY"]" foreach dir $dirs { catch {cd $dir} set c_files [glob -nocomplain c*] foreach name $c_files { puts "[format "%-15s %-20s " $name [pwd]]" } } :TEXT_END: