How to use the command 'basename' (with examples)
The ‘basename’ command is a command-line utility that is used to remove the leading directory portions from a given path. It is a part of the GNU Coreutils package and is included in most Unix-like operating systems. The ‘basename’ command is particularly useful when we want to extract specific information from a file or directory path.
Use case 1: Show only the file name from a path
Code:
basename path/to/file
Motivation: Sometimes, we may need to extract only the file name from a given path. This can be useful, for example, when we want to display or use the file name independently of the entire path.
Explanation: In this use case, the ‘basename’ command is used with the path to the file as the argument. It will remove the leading directory portions from the path and show only the file name.
Example output:
$ basename /home/user/Documents/file.txt
file.txt
In this example, the ‘basename’ command removes the leading directory portion ("/home/user/Documents/") from the given path and outputs only the file name (“file.txt”).
Use case 2: Show only the rightmost directory name from a path
Code:
basename path/to/directory/
Motivation: At times, we may want to extract the rightmost directory name from a full path. This can be beneficial, for instance, when we need to dynamically access or manipulate files within a particular directory.
Explanation: In this use case, the ‘basename’ command is used with the path to the directory as the argument, along with a trailing slash ("/"). It removes the leading directory portions from the path and shows only the rightmost directory name.
Example output:
$ basename /home/user/Documents/
Documents
Here, the ‘basename’ command eliminates the leading directory portion ("/home/user/") from the given path and returns only the rightmost directory name (“Documents”).
Use case 3: Show only the file name from a path, with a suffix removed
Code:
basename path/to/file suffix
Motivation: In certain cases, we might need to remove a specific suffix from a file name. This can be useful, for instance, when we want to change the file extension or when we need to filter files based on their suffix.
Explanation: In this use case, the ‘basename’ command is used with the path to the file as the first argument and the suffix to be removed as the second argument. It removes any trailing occurrence of the suffix from the file name and returns the modified file name.
Example output:
$ basename /home/user/Documents/file.tar.gz .tar.gz
file
Here, the ‘basename’ command removes the trailing “.tar.gz” suffix from the file name “file.tar.gz” and outputs “file”.
Conclusion:
The ‘basename’ command is a handy tool for manipulating file and directory paths in Unix-like operating systems. By removing the leading directory portions or specific suffixes, it allows users to extract key information from paths and work with them independently. Whether you need to display just the file name, extract the rightmost directory name, or remove a specific suffix, the ‘basename’ command can simplify these tasks effectively.