Extract filename and extension in Bash

后端 未结 30 1970
野趣味
野趣味 2020-11-21 05:29

I want to get the filename (without extension) and the extension separately.

The best solution I found so far is:

NAME=`echo \"$FILE\" | cut -d\'.\'          


        
30条回答
  •  盖世英雄少女心
    2020-11-21 06:27

    This is the only one that worked for me:

    path='folder/other_folder/file.js'
    
    base=${path##*/}
    echo ${base%.*}
    
    >> file
    

    This can also be used in string interpolation as well, but unfortunately you have to set base beforehand.

提交回复
热议问题