Extract filename and extension in Bash

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

    Ok so if I understand correctly, the problem here is how to get the name and the full extension of a file that has multiple extensions, e.g., stuff.tar.gz.

    This works for me:

    fullfile="stuff.tar.gz"
    fileExt=${fullfile#*.}
    fileName=${fullfile%*.$fileExt}
    

    This will give you stuff as filename and .tar.gz as extension. It works for any number of extensions, including 0. Hope this helps for anyone having the same problem =)

提交回复
热议问题