Extract filename and extension in Bash

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

    Usually you already know the extension, so you might wish to use:

    basename filename .extension
    

    for example:

    basename /path/to/dir/filename.txt .txt
    

    and we get

    filename
    

提交回复
热议问题