Shell programming skills: intercept file name or folder name from URL, file path

Intercepting file names or folder names from URLs and file paths is a very common operation. The general idea is to manipulate strings or even regular expressions to intercept the required parts, but in fact, as a very basic and common operation , the shell provides a nice command-line tool: basename to simplify this operation. Here's an example:

kafkaClientUrl="https://archive.apache.org/dist/kafka/3.5.1/kafka_2.12-3.5.1.tgz"
# 提取文件名(含后缀)
kafkaClientPkg=$(basename $kafkaClientUrl) # 得到:kafka_2.12-3.5.1.tgz
# 提取文件名(不含后缀),通常是解压后的文件夹名
kafkaClientDir=$(basename $kafkaClientUrl ".tgz")

Guess you like

Origin blog.csdn.net/bluishglc/article/details/132566914