Extract filename and extension in Bash

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

    From the answers above, the shortest oneliner to mimic Python's

    file, ext = os.path.splitext(path)
    

    presuming your file really does have an extension, is

    EXT="${PATH##*.}"; FILE=$(basename "$PATH" .$EXT)
    

提交回复
热议问题