如何获得目录名的在bash的最后一部分 [英] How to get the last part of dirname in bash
本文介绍了如何获得目录名的在bash的最后一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个文件 /from/here/to/there.txt
,并希望得到只是其目录名的最后一部分来
而不是从/这里/至
,我该怎么办?
Suppose I have a file /from/here/to/there.txt
, and want to get only the last part of its dirname to
instead of from/here/to
, what should I do?
推荐答案
您可以使用基本名
,即使它不是一个文件。使用目录名
脱光文件名,然后使用基本名
来获取字符串的最后一个元素:
You can use basename
even though it's not a file. Strip off the file name using dirname
, then use basename
to get the last element of the string:
dir="/from/here/to/there.txt"
dir="$(dirname $dir)" # Returns "/from/hear/to"
dir="$(basename $dir)" # Returns just "to"
这篇关于如何获得目录名的在bash的最后一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文