unix bash 得到文件的文件名和后缀名

原创文章,转载请注明来源并保留原文链接

basename example.tar.gz .tar.gz
# => example

FILE="example.tar.gz"

echo "${FILE%%.*}"
# => example

echo "${FILE%.*}"
# => example.tar

echo "${FILE#*.}"
# => tar.gz

echo "${FILE##*.}"
# => gz

# 在bash中可以这么写
filename=$(basename "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"

extract-filename-and-extension-in-bash

Leave a Reply

Your email address will not be published. Required fields are marked *