Last Updated: February 25, 2016
·
493
· crazyapi

Get filename and extension in Shell Script

#!/bin/bash
filePath="~/Desktop/GETTING THINGS DONE.pdf"
filename=$(basename "$filePath")
extension="${filename##*.}"
filename="${filename%.*}"
echo "$filename"
echo "$extension"

output:
FileName: GETTING THINGS DONE
FileExtension: pdf

See: Shell Parameter Expansion

1 Response
Add your response

Awesome! This also helped me understand the use of ## and % with the *.
Still lot more to learn...

over 1 year ago ·