Linux Command basename and 3 Options

The Linux command basename is useful when you need to extract only the filename from a file path or directory path. This command removes the last component of the path and returns just the filename. While it may seem simple, effectively using basename can make scripting and file management much more efficient.

What Is the Linux Command basename?

basename is a Linux command that retrieves the last component of a path. For example, if you have a path like /home/user/example.txt, using the basename command will return just example.txt. It is commonly used when file paths are long or when the filename is embedded in the path, allowing you to extract just the filename easily.

Figure 1. Basic usage of the Linux command basename
Figure 1. Basic usage of the Linux command basename

This command can be used in the following format: provide the path as an argument, and basename will return only the filename. You don’t need to write a separate script to parse the path and extract the filename; using basename makes it simple to retrieve the file name.

Basic Usage of basename

The basic usage of the Linux command basename is straightforward, as shown below. After the basename command, simply input the file path, and it will extract and return the filename.

basename /usr/bin/filename
ShellScript

In this example, basename extracts only filename from /usr/bin/filename. It is a quick way to get just the filename when you need it.

Useful Options

basename offers several useful options. By leveraging these options, you can handle more situations effectively.

-a, –multiple Option

This option allows you to process multiple file paths at once. Instead of executing the command repeatedly, you can handle multiple files or paths in a single command, making it more convenient.

basename -a /path/to/file1 /path/to/file2
ShellScript

The following is an example where we use the basename command with the -a option to extract both ls and mv from the /usr/bin directory.

Figure 2. Extracting multiple filenames simultaneously using the Linux command basename with the -a option
Figure 2. Extracting multiple filenames simultaneously using the Linux command basename with the -a option

-s, –suffix Option

Sometimes, when extracting filenames, you may want to exclude the file extension to get only the base filename. The -s option is used to remove a specified suffix (file extension) from the filename.

basename -s .txt /usr/local/bin/file.txt
ShellScript

As shown below, the .txt extension is removed, and only example is returned.

Figure 3. Removing the extension using the Linux command basename with the -s option
Figure 3. Removing the extension using the Linux command basename with the -s option

Another Way to Remove Extensions

You can remove extensions without using the -s option. By passing the extension as the last argument along with the path, you can exclude that part of the filename.

basename /home/ito/example.txt .txt
ShellScript

In this example, the .txt extension is removed, and only example is returned. This method allows you to easily remove unnecessary parts by specifying the extension. You don’t need to write a separate script to remove the extension—simply use basename with the second argument.

Figure 4. Removing the extension by passing it as the second argument to the Linux command basename
Figure 4. Removing the extension by passing it as the second argument to the Linux command basename

-z, –zero Option

The -z option in basename adds a NULL character (\0) at the end of the result instead of a newline character. By default, basename separates its output with a newline (\n) between each filename. However, the -z option adds a NULL character instead, reducing confusion when handling multiple filenames, especially when filenames contain spaces or special characters.

This option is particularly useful in scripts where you need to process multiple filenames and ensure each one is distinctly separated. The NULL character precisely marks the end of each filename, eliminating potential issues with spaces or line breaks.

Figure 5. Using the Linux command basename with the -z option to add a NULL character instead of a newline
Figure 5. Using the Linux command basename with the -z option to add a NULL character instead of a newline

As shown, the NULL character (\0) follows the filename example.txt instead of a newline.

Cautions When Using basename

While basename is a simple command, there are a few things to keep in mind when handling file paths.

  • Be mindful of path formats: If the path is incorrectly specified or a directory is included instead of a filename, you may not get the expected result. Always provide an accurate path that includes the filename.
  • Be precise with extensions: To remove an extension, make sure you specify the correct one. For example, if you try to remove .txt but the extension is actually .text, basename will not remove the extension properly.

Summary

basename is an incredibly useful command in Linux for handling paths and files. It allows you to extract just the filename from a path and can remove extensions or handle multiple paths at once. Especially when writing scripts or automating tasks, basename is essential for extracting filenames. By understanding and using its options effectively, you can tailor the command to suit your specific needs.

References

Leave a Comment