Writes to standard output all but the last part of a specified 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:
results in a single / (slash). Entering:
results in /a. Entering:
results in a single . (period). Entering:
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
|>0||An error occurred.|
To construct the name of a file located in the same directory as another, enter:
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.
|/usr/bin/dirname||Contains the dirname command.|
The basename command, sh command.