Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

csh(1)

ksh(1)

sh(1)

basename(1)  —  Commands

OSF

NAME

basename, dirname − Returns the base filename or directory portion of a pathname

SYNOPSIS

basename string [suffix]

dirname string

DESCRIPTION

The basename command reads the string specified on the command line, deletes the portion from the beginning to the last / (slash), and writes the base filename to standard output.  If suffix is specified on the command line and suffix appears in string, the string is returned with the suffix removed. 

The dirname command reads the string specified on the command line, deletes from the last / (slash) to the end of the line, and writes the remaining pathname to standard output. 

The basename and dirname commands are generally used inside command substitutions within a shell procedure to specify an output filename that is some variation of a specified input filename.  For more information, see the csh, ksh, and sh commands. 

EXAMPLES

     1.To display the base filename of a shell variable, enter:

basename $WORKFILE

This displays the base filename of the value assigned to the WORKFILE shell variable.  If WORKFILE is set to /u/gabe/program.c, then program.c is displayed. 

     2.To construct, in a shell script, a filename that is the same as another filename, except for its suffix, enter:

OFILE=‘basename $1 .c‘.o

This assigns to OFILE the value of the first positional parameter ($1), but with its .c suffix changed to .o.  If $1 is /u/jim/program.c, then OFILE becomes program.o.  Because program.o is only a base filename, it identifies a file in the current directory.  The “ (grave accents) perform command substitution. 

     3.To construct the name of a file located in the same directory as another, enter:

AOUTFILE=‘dirname $TEXTFILE‘/a.out

This sets the AOUTFILE shell variable to the name of an a.out file that is in the same directory as TEXTFILE.  If TEXTFILE is /u/fran/prog.c, then the value of dirname $TEXTFILE is /u/fran and AOUTFILE becomes /u/fran/a.out. 

RELATED INFORMATION

Commands:  csh(1), ksh(1), sh(1). 

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026