The basename
command in Linux is used to extract the base name or file name component from a given path or file name. It returns the file name without the directory portion.
Here’s an overview of how to use the basename
command:
- Basic Usage:
To extract the base name or file name from a path or file name, simply typebasename
followed by the path or file name. For example:
basename /path/to/file.txt
This command will return the file name component of the path, in this case, file.txt
.
- Examples:
- Extracting the base name from a file path:
basename /home/user/Documents/file.txt
This command will returnfile.txt
, which is the base name of the given file path. - Extracting the base name from a relative path:
basename ../directory/file.txt
This command will returnfile.txt
, which is the base name of the given relative path. - Extracting the base name from a file name:
basename file.txt
This command will returnfile.txt
, asfile.txt
does not contain a directory portion. - Removing a specific file extension:
basename file.txt .txt
This command will returnfile
, as it removes the specified.txt
extension from the file name.
The basename
command is useful when you need to extract the file name component from a given path or file name. It can be used in scripting or when working with file manipulation tasks.
For more information about the basename
command and its options, you can refer to the manual page by typing man basename
in your terminal.