How to Extract File Basename Without Path and Extension in Bash?

Linux works on a well-organized filesystem in a hierarchy of directories starting from the root(/). The filesystem can be easily accessed and managed through its absolute path, extension, and its basename, i.e., original filename. The file basename is useful to quickly identify the files for manipulating their file type and information, creating a new unique file, using it as an argument in a bash script, and so on.

This post illustrates the possible methods to extract file basename without its path as well as extension in Bash.

  • Using “basename” Command
  • Using Parameter Expansion (Without basename)

Method 1: Using “basename” Command

The built-in “basename” command line tool prints the file basename ignoring its prefix parameters(path) that end with “/(forward-slash)” and suffix(file extension). It displays the standard output in the form of a base file name.

Let’s see its practical implementation.

Create/Open a Bash Script

An existing “script.sh” is taken as an example opened in the “nano” text editor:

$ nano script.sh
#!/bin/bash

filename=/home/itslinuxfoss/File1.txt
s=$(basename $filename)
echo "${s%.*}"

The “script.sh” defines the following line of code:

  • The “#!/bin/bash” is the “Bash Shebang” that interprets the script in the bash shell.
  • The “filename” variable has an absolute path of the “File1.txt”.
  • The “s” variable encloses the “basename” command and the variable “filename” to extract the basename of “File1” from its specified path.
  • The “echo” statement contains a parameter expansion “${s%.*}” that removes all the character matches from “dot(.)” via “%.*”

Press the keyboard keys “Ctrl+S” for saving and “Ctrl+X” for exiting the editor from the terminal.

Execute the bash “script.sh” in this way:

./script.sh

The output shows only the base file name “File1.txt” without its path and extension.

Extract the File Basename From the URL

The “basename” command assists the user in getting the file basename also from the URL. It follows the same procedure as for “.txt,” “.pdf,” “.tar,” and many other file extensions. Let’s do this practically.

Bash Script

The “check.sh” bash script stores the following content:

#!/bin/bash

File_URL="http://example.com/CompressFile.tar" s=$(basename $File_URL)
echo "${s%.*}"

In the above “check.sh” the URL of the “CompressFile” file is assigned to the variable “File_URL.”

Save and close the script.

The execution of “check.sh” returns the file basename defined in it:

./check.sh

The File basename “CompressFile” has successfully shown in the terminal as standard output.

Method 2: Using Parameter Expansion(Without basename)

Another simplest way to extract the file basename is the “parameter expansion” without specifying the “basename” command. It uses the “##/” parameters that remove all the possible character matches to the file path.

Let’s see how it works in the bash script.

Bash Script

For instance, the “test.sh” bash script is taken as an example in the “nano” editor:

$ nano test.sh
#!/bin/bash

filename=/home/itslinuxfoss/Downloads/SampleFile.pdf       
s=${filename##*/}
echo "${s%.*}"

The “test.sh” script is described below:

  • The “filename” variable stores the absolute path of the “SampleFile.pdf”.
  • The “s” contains a parameter expansion in which the “##/” removes the file path by deleting the possible character matches followed by the “/(forward-slash)”.
  • The last parameter expansion removes the file extension and then prints the file basename using the “echo” command:

Run the script in the command line interface:

$ ./test.sh

The filename without path and extension has been extracted, i.e., “SampleFile”.

Conclusion

Linux offers the built-in “basename” command to extract file basename without path and extension. On the other hand, the file name can also be extracted with the help of the “parameter expansion” without using the “basename” command. These utilities remove the prefix, i.e., absolute file path, and the suffix, i.e., file extension, and return only its basename.

This guide has covered all possible methods to extract file basename without path and extension.