如何在 Bash 中获取 dirname 的最后一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23162299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:48:06  来源:igfitidea点击:

How to get the last part of dirname in Bash

bashshelldirname

提问by eggplantelf

Suppose I have a file /from/here/to/there.txt, and want to get only the last part of its dirname toinstead of /from/here/to, what should I do?

假设我有一个文件/from/here/to/there.txt,并且只想获取其目录名的最后一部分to而不是/from/here/to,我该怎么办?

回答by David W.

You can use basenameeven though it's not a file. Strip off the file name using dirname, then use basenameto get the last element of the string:

basename即使它不是文件,您也可以使用。使用 去除文件名dirname,然后使用basename获取字符串的最后一个元素:

dir="/from/here/to/there.txt"
dir="$(dirname $dir)"   # Returns "/from/here/to"
dir="$(basename $dir)"  # Returns just "to"

回答by jaypal singh

Using bashstring functions:

使用bash字符串函数:

$ s="/from/here/to/there.txt"
$ s="${s%/*}" && echo "${s##*/}"
to

回答by that other guy

The opposite of dirnameis basename:

的反义词dirnamebasename

basename "$(dirname "/from/here/to/there.txt")"

回答by codeforester

Using Bash parameter expansion, you could do this:

使用 Bash参数扩展,你可以这样做:

path="/from/here/to/there.txt"
dir="${path%/*}"       # sets dir      to '/from/here/to' (equivalent of dirname)
last_dir="${dir##*/}"  # sets last_dir to 'to' (equivalent of basename)

This is more efficient since no external commands are used.

由于不使用外部命令,因此效率更高。

回答by anubhava

Pure BASH way:

纯 BASH 方式:

s="/from/here/to/there.txt"
[[ "$s" =~ ([^/]+)/[^/]+$ ]] && echo "${BASH_REMATCH[1]}"
to

回答by iruvar

One more way

另一种方式

IFS=/ read -ra x <<<"/from/here/to/there.txt" && printf "%s\n" "${x[-2]}"

回答by csiu

An awkway of doing it would be:

一种awk方法是:

awk -F'/' '{print $(NF-1)}' <<< "/from/here/to/there.txt"

Explanation:

解释:

  • -F'/'sets field separator as "/"
  • print the second last field $(NF-1)
  • <<<uses anything after it as standard input (wiki explanation)
  • -F'/'将字段分隔符设置为“/”
  • 打印倒数第二个字段 $(NF-1)
  • <<<使用它之后的任何内容作为标准输入(维基解释

回答by MLSC

This question is something like THIS.

这个问题有点像

For solving that you can do:

为了解决这个问题,你可以这样做:

DirPath="/from/here/to/there.txt"
DirPath="$(dirname $DirPath)"
DirPath="$(basename $DirPath)"

echo "$DirPath"

As my friend said this is possible as well:

正如我的朋友所说,这也是可能的:

basename `dirname "/from/here/to/there.txt"`

In order to get any part of your path you could do:

为了获得路径的任何部分,您可以执行以下操作:

echo "/from/here/to/there.txt" | awk -F/ '{ print  }'
OR
echo "/from/here/to/there.txt" | awk -F/ '{ print  }'
OR
etc

回答by yashma

The top answer is absolutely correct for the question asked. In a more generic case with the needed directory in the middle of a long path, this approach leads to a hard to read code. For example :

对于提出的问题,最高答案是绝对正确的。在需要的目录位于长路径中间的更通用的情况下,这种方法会导致难以阅读的代码。例如 :

dir="/very/long/path/where/THIS/needs/to/be/extracted/text.txt"
dir="$(dirname $dir)"
dir="$(dirname $dir)"
dir="$(dirname $dir)"
dir="$(dirname $dir)"
dir="$(dirname $dir)"
dir="$(basename $dir)"

In this case one can use:

在这种情况下,可以使用:

IFS=/; set -- "/very/long/path/where/THIS/needs/to/be/extracted/text.txt"; set ; echo 
THIS