Extract filename and extension in Bash

后端 未结 30 2617
野趣味
野趣味 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条回答
  •  萌比男神i
    2020-11-21 06:08

    Here is code with AWK. It can be done more simply. But I am not good in AWK.

    filename$ ls
    abc.a.txt  a.b.c.txt  pp-kk.txt
    filename$ find . -type f | awk -F/ '{print $2}' | rev | awk -F"." '{$1="";print}' | rev | awk 'gsub(" ",".") ,sub(".$", "")'
    abc.a
    a.b.c
    pp-kk
    filename$ find . -type f | awk -F/ '{print $2}' | awk -F"." '{print $NF}'
    txt
    txt
    txt
    

提交回复
热议问题