The dirname Command
The dirname
command is a built-in shell command that returns the directory portion of a path. The dirname
utility deletes the filename portion, beginning with the last slash ‘/’ character to the end of string. You can use it for:
- Extracting the directory path from a file path.
- Removing the filename from a path.
- Generating directory paths for temporary files.
Syntax
The dirname
command takes a single argument, which is the path to the file or directory whose directory path you want to extract. The command returns the directory path, minus the filename.
dirname <path>
Examples
Extract the directory path from a file path:
dirname /home/user/myfile.txt
Output: /home/user
Create a temp file and return its directory path:
dirname $(mktemp).tmp)
Output: /var/folders/fk/_z2xh0951lvc_d88hvq59__h0000gn/T
With gencmd
gencmd how do I get the dir path from path
- dirname [path]