Writes to standard output all but the last part of a specified path.
dirname Path
The dirname command reads the specified path name, deletes all but the last / (slash) and the characters following it, and writes the result to standard output. If no characters follow the last /, the dirname command uses the next to last / and ignores all characters following it. The dirname command applies the following rules in creating the path name:
For example, entering:
dirname //
results in a single / (slash). Entering:
dirname /a/b/
results in /a. Entering:
dirname a
results in a single . (period). Entering:
dirname a/b
results in the path name a.
The dirname and basename commands are generally used inside command substitutions within a shell procedure to specify an output file name that is some variation of a specified input file name.
This command returns the following exit values:
Item | Description |
---|---|
0 | Successful completion |
>0 | An error occurred. |
To construct the name of a file located in the same directory as another, enter:
AOUTFILE=`dirname $TEXTFILE`/a.out
This sets the shell variable AOUTFILE to the name of an a.out file that is in the same directory as TEXTFILE. If TEXTFILE is /home/fran/prog.c, the value of dirname $TEXTFILE is /home/fran and AOUTFILE becomes /home/fran/a.out.
Item | Description |
---|---|
/usr/bin/dirname | Contains the dirname command. |