The 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 dirname command:
- Basic Usage:
To extract the directory name from a path or file name, simply typedirnamefollowed by the path or file name. For example:
dirname /path/to/file.txt
This command will return the directory component of the path, in this case, /path/to.
- Examples:
- 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:
dirname file.txt
This command will return.(current directory), asfile.txtdoes not contain a directory component.
The 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.