basename Command in Linux



Having trouble with file path management in your command line shell? You’re not alone. Many developers face challenges when it comes to managing file paths. Fortunately, there is a tool that can guide you through this maze.

The basename command is an effective tool that can help you navigate the maze of directories, much like a proficient navigator. It extracts the last component of a file path or URL.

This handy command also removes any leading directory components and optionally eliminates a trailing suffix (such as a file extension). It’s particularly useful in shell scripts for manipulating file and directory names.

Table of Contents

Syntax of basename Command

The following is a basic syntax that highlights how you can use the basename command −

basename NAME [SUFFIX]

Or,

basename OPTION NAME

By default, basename extracts the filename component from the provided path. You can also employ options such as the "-s" flag to remove a trailing suffix or the "-a" flag to support multiple arguments.

In addition, if you want to remove the suffix from a file, you can enter the filename followed by the name of the SUFFIX you want to remove.

Different Options Available for basename Command

Tag Description
-a or --multiple This option allows basename to support multiple arguments. When used, each argument is treated as a separate name, and the command extracts the filename component from each one.
-s or --suffix=SUFFIX This option allows basename to remove a trailing suffix (such as a file extension) from the given name. It implies the "-a" option, so it also works with multiple arguments.
-z or --zero Instead of ending each output line with a newline character, this option ends it with a NUL character (useful for scripting).
--help Displays the help message for basename and exits.
--version Outputs version information for basename and exits.

Examples of basename Command

The following are various operations you can perform with the basename command.

Using the basename command to Extract a File Name From a File Path

Consider you have a file in this location "/home/user/data/myfile.txt". If you want to extract just the file name ‘myfile.txt’ from this path, run the following command in your terminal −

basename /home/user/data/myfile.txt
Extract a File Name from a File Path 1

In this example, ‘basename’ strips away the ‘/home/user/data/’ path, leaving us with ‘myfile.txt’.

One advantage of using the ‘basename’ is its ease of use and simplicity. However, it's important to be aware that 'basename' doesn't verify whether the file or directory exists. It does nothing more than process the text string you provide it.

So, if you were to run this command basename "/home/user/document/data", it would still return ‘data’ as the output, even though there’s no such file or directory.

basename /home/user/document/data
Extract a File Name from a File Path 2

Removing Extensions with the Basename Command

The basename command is typically used for extracting a file name from a specified path. You can also this command-line utility to remove an extension while extracting the file name.

To get started, simply enter what you want to remove from the end of the output. Let's say you want to remove the .ssh from the filename.ssh, simply add it at the end of the basename command −

basename /home/user/documents/filename.ssh .ssh
Removing Extensions with Basename Command

Using the "-s" Option and Suffix Handling

You can also the basename command with the "-s" flag to remove a suffix from a file path. Here is how you can get started.

basename -s .txt /home/group/data/report.txt
Using the -s Option and Suffix Handling 1

The "-s" flag specifies that we want to remove the trailing .txt suffix from the provided path. If also you enter txt (without the dot) instead of .txt, you’ll get ‘report.’ (with the dot at the end).

In addition, if you enter a suffix that is not at the end of the component, the output remains as if there was no suffix.

basename /home/user/data/filename.txt text
Using the -s Option and Suffix Handling 2

Using the "-a" Option to Handle Multiple Inputs

The basename command only accepts one input by default. For instance −

basename /home/user/file.txt /home/user/file1.txt
Using the -a Option to Handle Multiple Inputs 1

When you include the "-a" flag in the above syntax, it tells the basename command to accept multiple filenames as arguments and handle each input independently.

basename -a /home/user/file.txt /home/user/file1.txt
Using the -a Option to Handle Multiple Inputs 2

You can also use the "-s" and "-a" flags together but with some limitations. You can only enter one suffix to all the file paths.

basename -as .txt /home/document/myfile1.txt /home/user/document/myfile2.txt
Using the -a Option to Handle Multiple Inputs 3

You can't assign individual suffices to the two file paths, It won’t work.

Using the "-z" Option to Handle Special Characters

Up to this point, we’ve covered how basename is commonly used. The examples above show that basename uses newline characters as its default delimiters in the output.

While this default behavior works well in most cases, it can be confusing when dealing with input containing special characters −

basename -a /home/user/$'myfile\t1'.txt /home/user/$'myfile\t2'.txt
-z Option to Handle Special Characters 1

In the example above, I've included two inputs containing newline characters (\t). I also used the $ character to quote their names.

As a result, the basename's output shows four lines. This makes it difficult to determine which value goes with each input.

To solve this issue, you can use the "-z" option to tell basename to delimit output with NULL characters rather than newlines.

To also display the NULL characters, you can pipe the basename‘s result to the cat command with the "-v" option, which prints NULL as ^@ −

basename -az /home/user/$'myfile\t1'.txt /home/user/$'myfile\t2'.txt | cat -v
-z Option to Handle Special Characters 2

Using basename in bash script

The basename command in Bash is useful for extracting the filename from a file path. Consider you have a file path stored in a variable called filepath.

You can use the following script to extract the filename −

filepath="/home/user/data/filename.txt"
filename=$(basename "$filepath")
echo "The filename is: $filename"
Using basename in bash script

In the above script, basename extracts the filename from the filepath. The result is stored in the filename variable and the echo command displays the extracted filename.

You can decide to replace the filepath value with your actual file path. If also you want to remove a specific file extension like .txt you can follow the examples above.

Conclusion

The dirname command is a complement to the basename command. The dirname command prints the entire path, excluding the final component, in contrast to the basename.

In this tutorial, we presented a few examples that you can use to get the best out of the basename command.

Advertisements