dirname command in Linux is used to extract the directory component from a given path or file name. It returns the directory name without the filename portion.
Here’s an overview of how to use the
- Basic Usage:
To extract the directory name from a path or file name, simply type
dirnamefollowed by the path or file name. For example:
This command will return the directory component of the path, in this case,
- Extracting the directory from a file path:
dirname /home/user/Documents/file.txtThis command will return
/home/user/Documents, which is the directory component of the given file path.
- Extracting the directory from a relative path:
dirname ../directory/file.txtThis command will return
../directory, which is the directory component of the given relative path.
- Extracting the directory from a file name:
This command will return
.(current directory), as
file.txtdoes not contain a directory component.
dirname command is useful when you need to extract the directory portion from a given path or file name. It can be helpful in scripting or when working with file manipulation tasks.
For more information about the
dirname command and its options, you can refer to the manual page by typing
man dirname in your terminal.