Extract filename and extension in Bash

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

    You can use basename.

    Example:

    $ basename foo-bar.tar.gz .tar.gz
    foo-bar
    

    You do need to provide basename with the extension that shall be removed, however if you are always executing tar with -z then you know the extension will be .tar.gz.

    This should do what you want:

    tar -zxvf $1
    cd $(basename $1 .tar.gz)
    

提交回复
热议问题