The Linux dirname command is used to output the name with the trailing "/" character part removed.
Linux dirname command Function Description
Use the dirname command to output the name with the trailing "/" character removed; if the name does not contain a "/", "." is displayed (indicates the current directory).
The dirname command removes the non-directory part of the file name and displays only the contents related to the directory. dirname command reads the specified pathname retaining the last /
and the characters after it, removes the rest, and writes the result to the standard output. If there are no characters after the last /
, the dirname command uses the penultimate /
and ignores all subsequent characters. dirname and basename are usually used interchangeably in shell internal commands to specify an output file name that differs slightly from the specified input file name.
Linux dirname command Syntax
dirname [name]
dirname [option]
Linux dirname command Examples
Common examples
dirname //
result is: /
dirname /a/b/
result is: /a
dirname a
result is: .
dirname a/b
result is: a
Name of the ccc.txt file output with the trailing "/" character part removed
dirname ccc.txt
Output:
Name for the output of /root/install.log file without the trailing "/" character part
[root@rhel ~]# dirname /root/install.log
/root