Description of Use
The dirname command can take the directory portion of a given path (strip non-directory suffix from file name). This command is rarely used directly from the shell command line, and I typically use it in a shell script to get the directory where the script file is located and then switch the current directory over. According to the manual page, "Print NAME with its trailing/component removed; If NAME contains no/' s, Output '. ' (meaning the current directory). "It seems that" taking the directory portion of a given path "does not accurately summarize the purpose of the dirname command. Another command under Linux is basename, which, contrary to dirname, is the part that gets the file name.
Common parameters
No.
Example of using a sample from a man page
[Email protected] ~]# Dirname/usr/bin/sort
/usr/bin
[Email protected] ~]# dirname stdio.h
.
[Email protected] ~]#
Example Two
[Email protected] ~]# Dirname/usr/bin
/usr
[Email protected] ~]# dirname/usr/bin/
/usr
Note: The output here seems a bit strange. This should take a look at the source code of the DirName command to explain.
Example three using bash code in a bash script
- #!/bin/sh
- # Jump to the directory where the script is located
- CD $ (dirname "$") | | exit 1
- # The rest of the part
I often use this notation because sometimes the script does not know which directory it is in, such as when the script is used in crontab.
Another notation is: CD ' DirName $ ' where the anti-quote · Equivalent to $ ().
Shell command---dirname