在Bash中获取目录的父目录



如果我有一个文件路径,如…

/home/smith/Desktop/Test
/home/smith/Desktop/Test/

我如何改变字符串,使它将是父目录?

/home/smith/Desktop
/home/smith/Desktop/
dir=/home/smith/Desktop/Test
parentdir="$(dirname "$dir")"

显然父目录是通过简单地附加点。点filename来给出的:

/home/smith/Desktop/Test/..     # unresolved path

但是你必须想要解析路径(一个没有任何点-点路径组件的绝对路径):

/home/smith/Desktop             # resolved path

使用dirname的顶部答案的问题是,当您输入带有点的路径时,它们不起作用:

$ dir=~/Library/../Desktop/../..
$ parentdir="$(dirname "$dir")"
$ echo $parentdir
/Users/username/Library/../Desktop/..   # not fully resolved

这个功能更强大:

dir=/home/smith/Desktop/Test
parentdir=$(builtin cd $dir; pwd)

你可以给它/home/smith/Desktop/Test/..,但也可以更复杂的路径,如:

$ dir=~/Library/../Desktop/../..
$ parentdir=$(builtin cd $dir; pwd)
$ echo $parentdir
/Users                                  # the fully resolved path!
 

注意:使用builtin确保不调用cd的自定义函数变体,而是默认的没有输出的实用程序形式。

在您想要查找父目录的目录中工作时使用echo $(cd ../ && pwd)。这个链还有一个额外的好处,那就是没有后面的斜杠。

…但"这里看到的"是破碎的。修复方法如下:

> pwd
/home/me
> x='Om Namah Shivaya'
> mkdir "$x" && cd "$x"
/home/me/Om Namah Shivaya
> parentdir="$(dirname "$(pwd)")"
> echo $parentdir
/home/me

另一个答案的动机

我喜欢短小、清晰、有保证的代码。如果它不运行外部程序,那么它的好处是,因为当您需要处理大量条目时,它将明显更快。

<标题> 原则

不确定你有什么保证,想要什么,所以还是提供。

如果你有保证,你可以用很短的代码做到这一点。这个想法是使用bash文本替换功能来删除最后一个斜杠和后面的内容。

回答由简单到复杂的原始问题。

如果路径保证没有任何斜杠结束

P=/home/smith/Desktop/Test ; echo "${P%/*}"
/home/smith/Desktop

如果路径保证以一个斜杠(in和out)结束

P=/home/smith/Desktop/Test/ ; echo "${P%/*/}/"
/home/smith/Desktop/

如果输入路径可能以零或一个斜杠结束,并且您希望输出路径不以斜杠结束

for P in 
    /home/smith/Desktop/Test 
    /home/smith/Desktop/Test/
do
    P_ENDNOSLASH="${P%/}" ; echo "${P_ENDNOSLASH%/*}"
done
/home/smith/Desktop
/home/smith/Desktop

如果输入路径可能有许多多余的斜杠,并且您希望输出路径不以斜杠结束

for P in 
    /home/smith/Desktop/Test 
    /home/smith/Desktop/Test/ 
    /home/smith///Desktop////Test// 
do
    P_NODUPSLASH="${P///*(/)//}"
    P_ENDNOSLASH="${P_NODUPSLASH%%/}"
    echo "${P_ENDNOSLASH%/*}";   
done
/home/smith/Desktop
/home/smith/Desktop
/home/smith/Desktop

如果您只需要父目录的名称:

parent_dir_name=$(basename $(dirname $PWD))

如果/home/smith/Desktop/Test/../是你想要的:

dirname 'path/to/child/dir'

根据您是否需要绝对路径,您可能需要采取额外的步骤:

child='/home/smith/Desktop/Test/'
parent=$(dirname "$child")
abs_parent=$(realpath "$parent")

如果您想要第四个父目录,请使用:export MYVAR="$(dirname "$(dirname "$(dirname "$(dirname $PWD)")")")"

export MYVAR="$(dirname "$(dirname "$(dirname $PWD)")")"如果你想要第三个父目录

export MYVAR="$(dirname "$(dirname $PWD)")"如果你想要第二个父目录

丑陋但高效

function Parentdir()

{

local lookFor_ parent_ switch_ i_
lookFor_="$1"
#if it is not a file, we need the grand parent
[ -f "$lookFor_" ] || switch_="/.."
#length of search string
i_="${#lookFor_}"
#remove string one by one until it make sens for the system
while [ "$i_" -ge 0 ] && [ ! -d "${lookFor_:0:$i_}" ];
do
    let i_--
done
#get real path
parent_="$(realpath "${lookFor_:0:$i_}$switch_")" 
#done
echo "
lookFor_: $1
{lookFor_:0:$i_}: ${lookFor_:0:$i_}
realpath {lookFor_:0:$i_}: $(realpath ${lookFor_:0:$i_})
parent_: $parent_ 
"

}

    lookFor_: /home/Om Namah Shivaya
{lookFor_:0:6}: /home/
realpath {lookFor_:0:6}: /home
parent_: /home 

lookFor_: /var/log
{lookFor_:0:8}: /var/log
realpath {lookFor_:0:8}: /UNIONFS/var/log
parent_: /UNIONFS/var 

lookFor_: /var/log/
{lookFor_:0:9}: /var/log/
realpath {lookFor_:0:9}: /UNIONFS/var/log
parent_: /UNIONFS/var 

lookFor_: /tmp//res.log/..
{lookFor_:0:6}: /tmp//
realpath {lookFor_:0:6}: /tmp
parent_: / 

lookFor_: /media/sdc8/../sdc8/Debian_Master//a
{lookFor_:0:35}: /media/sdc8/../sdc8/Debian_Master//
realpath {lookFor_:0:35}: /media/sdc8/Debian_Master
parent_: /media/sdc8 

lookFor_: /media/sdc8//Debian_Master/../Debian_Master/a
{lookFor_:0:44}: /media/sdc8//Debian_Master/../Debian_Master/
realpath {lookFor_:0:44}: /media/sdc8/Debian_Master
parent_: /media/sdc8 

lookFor_: /media/sdc8/Debian_Master/../Debian_Master/For_Debian
{lookFor_:0:53}: /media/sdc8/Debian_Master/../Debian_Master/For_Debian
realpath {lookFor_:0:53}: /media/sdc8/Debian_Master/For_Debian
parent_: /media/sdc8/Debian_Master 

lookFor_: /tmp/../res.log
{lookFor_:0:8}: /tmp/../
realpath {lookFor_:0:8}: /
parent_: /

从想法开始/评论Charles Duffy - Dec 17 '14 at 5:32在Bash脚本中获取当前目录名(没有完整路径)

#!/bin/bash
#INFO : https://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-a-bash-script
# comment : by Charles Duffy - Dec 17 '14 at 5:32
# at the beginning :

declare -a dirName[]
function getDirNames(){
dirNr="$(  IFS=/ read -r -a dirs <<<"${dirTree}"; printf '%sn' "$((${#dirs[@]} - 1))"  )"
for(( cnt=0 ; cnt < ${dirNr} ; cnt++))
  do
      dirName[$cnt]="$( IFS=/ read -r -a dirs <<<"$PWD"; printf '%sn' "${dirs[${#dirs[@]} - $(( $cnt+1))]}"  )"
      #information – feedback
      echo "$cnt :  ${dirName[$cnt]}"
  done
}
dirTree=$PWD;
getDirNames;

如果出于某种原因您对导航到特定数量的目录感兴趣,您也可以这样做:nth_path=$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd ../../../ && pwd)。这将给出3个父目录

这将进入父文件夹

cd ../

相关内容

  • 没有找到相关文章

最新更新