technology as-is

Linux : Split Name and Extension from filename


If is just filename  (samplefile.txt) then follow these steps.

$ fullname=”samplefile.txt”

$ extension=”${fullname##*.}”

$ filename=”${fullname%.*}”

$ echo $extension

result : txt

$ echo $filename

result : samplefile


If filename is part of path  (/usr/gchan/samplefile.txt)

$ fullname =”/usr/gchan/samplefile.txt”

# Remove the path and get only  “samplefile.txt”

$ fullname=”${fullname##*/}”  

$ echo $fullname

result : samplefile.txt

# Now repeat the same steps mentioned above to split the name and extension. If above step is not done, then filename will contain  /usr/gchan/samplefile  instead of  samplefile.

$ extension=”${fullname##*.}”

$ filename=”${fullname%.*}”




Leave a Reply

Required fields are marked *.