<tt>dirname</tt>: Directory Path



next up previous contents
Next: stty -echo: Passwords Up: Tips Using Shell Previous: basename: Current Directory

dirname: Directory Path

dirname directory
will return the path of the current directory from the beginning of the directory name to the last ``/".

For example,

dirname /usr/people/cantin
will return /usr/people. But
dirname cantin
will return . (dot).

For example, the following script will take as its first parameter, the name of a directory, and return its absolute path name. The directory must be given either as a relative or absolute path name.

#!/bin/sh 
# Returns full path name of a directory.
if [ $# -ne 1 ]
then
    echo " "
    echo " $0: must have one parameter."
    echo " "
    exit
fi

C_DIR=`pwd`
if test "`dirname $1`" = "."
then
    FULL_NAME=$C_DIR/$1
else
    FULL_NAME=$1
fi
echo "Absolute path name is $FULL_NAME."



Super-User
Fri Feb 17 15:55:40 EST 1995