Specific use of Linux dirname command

  • 2021-07-26 09:07:10
  • OfStack

01. Command Overview

dirname-Stripping non-directory suffixes from file names

The dirname command removes the non-directory part of the file name and displays only the contents related to the directory. The dirname command reads the specified pathname, preserves the last 1/and the following characters, deletes the rest, and writes the result to standard output. If there is no character after the last 1/, the dirname command uses the penultimate/and ignores all subsequent characters. dirname and basename are commonly used in substitution with shell internal commands to specify an output file name slightly different from the specified input file name.

02. Command format


dirname  Name 
dirname  Options 

03. Common Options

Print NAME with/removed; If NAME does not contain/, output `. '(indicating the current directory).


--help 
  Show Help and Exit 
--version
  Output version information and exit 

04. Reference example

4.1 If the last file is a directory


[deng@localhost test]$ dirname /home/deng/share/
/home/deng

4.2 If the last file is a normal file case


[deng@localhost test]$ dirname /home/deng/scott_data.sql 
/home/deng

4.3 Output if/is not included in the name.


[deng@localhost test]$ dirname dir
.
[deng@localhost test]$ 

4.4 Relative path case


[deng@localhost test]$ dirname dir/a
dir

4.5 Case where the path is the root directory


[deng@localhost test]$ dirname /
/
[deng@localhost test]$ dirname //
/
[deng@localhost test]$ 

Related articles: