Extract filename and extension in Bash

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

    Mellen writes in a comment on a blog post:

    Using Bash, there’s also ${file%.*} to get the filename without the extension and ${file##*.} to get the extension alone. That is,

    file="thisfile.txt"
    echo "filename: ${file%.*}"
    echo "extension: ${file##*.}"
    

    Outputs:

    filename: thisfile
    extension: txt
    

提交回复
热议问题