In a Bash script, you may need to extract the filename and extension and save them into variables. This tutorial shows how to achieve this for a given file.

This article covers

  • Extract the file name for a given full path
  • getting the full file name
  • Extension name

Extract file name with extension

To obtain the filename along with its extension, the basename command can be used to remove the directory and return only the filename for the given path, whether it is a variable or a string.

For instance, if the path is /home/john/run.sh, the returned filename would be run.sh. This process involves taking the full path and extracting only the filename by removing the path. The resulting filename is then stored in a variable and printed to the console.

the basename is used to remove the directory and return the filename for the given path. the path is variable or string. For example, the path is /home/john/run.sh, and returned file name is run.sh In this, the full path is given and returns the filename by removing the path.

The filename is saved to a variable and printed to the console.

file_path="/home/john/run.sh"
filename=$(basename "$file_path")
echo $filename

output:

run.sh

Extract filename without extension

To obtain only the filename without the extension, you can utilize the ${filename%.*} syntax.

For example, consider the path /home/john/run.sh the resulting filename would be run.sh.

Initially, the basename command is employed to eliminate the directory and yield the filename for the specified path and returns a variable , and this variable is then utilized in conjunction with the expression syntax to strip off the extension from the filename.

This expression effectively removes the extension from the filename.

filepath="/home/john/run.sh"
filename=$(basename "$filepath")
echo $filename
echo "File Name: ${filename%.*}"

output:

File Name: run

Extract extension for a file path

To isolate the file extension from the given file path, ${filename##*.} can be utilized. This expression returns the file extension only.

For example, consider the path /home/john/run.sh the resulting extension would be sh.

Initially, the basename command is used to remove the directory path and return the filename for the specified path, and this filename is then utilized in conjunction with the expression syntax to return only the extension.

filepath="/home/john/run.sh"
filename=$(basename "$filepath")
echo $filename
echo "File Extension: ${filename##*.}"

Output

File Extension:sh

Here is a comprehensive example that showcases how to obtain a filename with or without a file extension. Upon executing the script below, it prints

  • the file with an extension,
  • only the file name without an extension,
  • and the extension alone.

Example:

filepath="/home/username/run.sh"
filename=$(basename "$filepath")
echo "File :$filename"
echo "File Name: ${filename%.*}"
echo "File Extension: ${filename##*.}"

Output

File:run.sh
File Name:run
File Extension:sh

Conclusion

This script shows a multiple approach for handling filenames and extensions in a Bash environment.