HomeLinux

Linux : Split Name and Extension from filename

Like Tweet Pin it Share Share Email

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%.*}”

 

 

 

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *