Extract filename and extension in Bash

后端 未结 30 2044
野趣味
野趣味 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:13

    ~% FILE="example.tar.gz"
    
    ~% echo "${FILE%%.*}"
    example
    
    ~% echo "${FILE%.*}"
    example.tar
    
    ~% echo "${FILE#*.}"
    tar.gz
    
    ~% echo "${FILE##*.}"
    gz
    

    For more details, see shell parameter expansion in the Bash manual.

提交回复
热议问题