Skip to content

Linux basename command

The Linux basename command is used to strip directory and suffix from filenames. It is a handy tool for manipulating file paths and extracting specific parts of a file name. By using this command, you can easily isolate the base name of a file, which can be useful for various scripting and automation tasks. The basename command can be customized to remove a specified suffix or only display a specific part of the file name. This command is particularly helpful when working with large numbers of files or when needing to process file names in a consistent manner. Familiarizing yourself with the basename command can streamline your workflow and make file manipulation tasks more efficient.

Terminal window
basename [option] [parameter]
OptionDescription
-aSupport multiple arguments
-sRemove the suffix STRING
-zSeparate output by NULL byte
Treat all further arguments as file names (even if they start with ’-‘)
ParameterDescription
stringThe string from which to extract base name
pathThe path for which to extract the base name
Terminal window
basename /home/user/Documents/example.txt

This command will output “example.txt”.

Terminal window
basename /folder/document.pdf .pdf

The output will be “document”.

Terminal window
basename /home/user/Documents/

Will return “Documents”.

Terminal window
basename /home/user/Documents/example/file.txt

The result will be “file.txt”.

To use the basename command in Linux, execute the following command:

Terminal window
basename /path/to/file.txt

How can I get the filename without its full path using basename?

Section titled “How can I get the filename without its full path using basename?”

To extract just the filename without the directory path, you can use the basename command with the file path as shown below:

Terminal window
basename /home/user/documents/report.txt

How do I remove the file extension using basename in Linux?

Section titled “How do I remove the file extension using basename in Linux?”

To remove the file extension from a filename, you can use the basename command along with the appropriate options. Here’s an example:

Terminal window
basename -s .txt document.txt

How can I remove multiple file extensions using basename?

Section titled “How can I remove multiple file extensions using basename?”

You can remove multiple file extensions from a filename using the basename command with the -a option. Here’s an example:

Terminal window
basename -a -s .txt -s .bak backup_file.txt

How can I remove a suffix from a filename using basename?

Section titled “How can I remove a suffix from a filename using basename?”

To remove a specific suffix from a filename, you can use the basename command with the -s option followed by the suffix to be removed. Here’s an example:

Terminal window
basename -s .txt document.txt

How do I use basename to extract the directory path of a file?

Section titled “How do I use basename to extract the directory path of a file?”

If you need to extract the directory path of a file using basename, you can do so by providing the full file path. Here’s an example:

Terminal window
basename /home/user/documents/report.txt

How can I handle filenames with spaces when using basename in Linux?

Section titled “How can I handle filenames with spaces when using basename in Linux?”

When working with filenames that contain spaces, make sure to enclose the file path in quotes to avoid any issues. Here’s an example:

Terminal window
basename "/home/user/my documents/report.txt"
  1. Stripping directory and suffix from filenames
  2. Parsing filenames in shell scripts
  3. Manipulating file paths in scripts
  4. Extracting a specific part of a file path