Linux 利用命令输入的Bash别名

Linux 利用命令输入的Bash别名,linux,bash,Linux,Bash,我想创建一个bash别名来执行以下操作: 假设我在以下路径: /dir1/dir2/dir3/…../dirN 我想不使用cd..直接升级到dir3。我将只编写cddir3,它应该直接转到/dir1/dir2/dir3cdd是我的别名 我编写了以下别名,但它不起作用: alias cdd='export newDir=$1; export myPath=`pwd | sed "s/\/$newDir\/.*/\/$newDir/"`; cd $myPath' 简单地说,它应该获得当前的完整路径

我想创建一个bash别名来执行以下操作:

假设我在以下路径:

/dir1/dir2/dir3/…../dirN

我想不使用
cd..
直接升级到dir3。我将只编写
cddir3
,它应该直接转到
/dir1/dir2/dir3
cdd
是我的别名

我编写了以下别名,但它不起作用:

alias cdd='export newDir=$1; export myPath=`pwd | sed "s/\/$newDir\/.*/\/$newDir/"`; cd $myPath'
简单地说,它应该获得当前的完整路径,然后删除新目标目录之后的任何内容,然后cd到这个新路径


我的命令的问题是,
$1
无法将我的输入获取到命令
cdd

这里有一个函数,您可以将它放在shell配置文件中,它可以满足您的需要;注意,除了目录名之外,它还支持级别(例如,
cdd2
在层次结构中向上提升两个级别);只需使用
cdd
即可上移到父目录

还要注意,匹配不区分大小写

代码取自“”,您还可以在其中找到一种方法,为祖先目录名添加互补的制表符补全

    cdd () 
    { 
        local dir='../';
        [[ "$1" == '-h' || "$1" == '--help' ]] && { 
            echo -e "usage:
        $FUNCNAME [n]
        $FUNCNAME dirname
      Moves up N levels in the path to the current working directory, 1 by default.
      If DIRNAME is given, it must be the full name of an ancestral directory (case does not matter).
      If there are multiple matches, the one *lowest* in the hierarchy is changed to." && return 0
        };
        if [[ -n "$1" ]]; then
            if [[ $1 =~ ^[0-9]+$ ]]; then
                local strpath=$( printf "%${1}s" );
                dir=${strpath// /$dir};
            else
                if [[ $1 =~ ^/ ]]; then
                    dir=$1;
                else
                    local wdLower=$(echo -n "$PWD" | tr '[:upper:]' '[:lower:]');
                    local tokenLower=$(echo -n "$1" | tr '[:upper:]' '[:lower:]');
                    local newParentDirLower=${wdLower%/$tokenLower/*};
                    [[ "$newParentDirLower" == "$wdLower" ]] && { 
                        echo "$FUNCNAME: No ancestral directory named '$1' found." 1>&2;
                        return 1
                    };
                    local targetDirPathLength=$(( ${#newParentDirLower} + 1 + ${#tokenLower} ));
                    dir=${PWD:0:$targetDirPathLength};
                fi;
            fi;
        fi;
        pushd "$dir" > /dev/null
    }

这里有一个函数,你可以把它放在你的shell配置文件中,它可以满足你的需要;注意,除了目录名之外,它还支持级别(例如,
cdd2
在层次结构中向上提升两个级别);只需使用
cdd
即可上移到父目录

还要注意,匹配不区分大小写

代码取自“”,您还可以在其中找到一种方法,为祖先目录名添加互补的制表符补全

    cdd () 
    { 
        local dir='../';
        [[ "$1" == '-h' || "$1" == '--help' ]] && { 
            echo -e "usage:
        $FUNCNAME [n]
        $FUNCNAME dirname
      Moves up N levels in the path to the current working directory, 1 by default.
      If DIRNAME is given, it must be the full name of an ancestral directory (case does not matter).
      If there are multiple matches, the one *lowest* in the hierarchy is changed to." && return 0
        };
        if [[ -n "$1" ]]; then
            if [[ $1 =~ ^[0-9]+$ ]]; then
                local strpath=$( printf "%${1}s" );
                dir=${strpath// /$dir};
            else
                if [[ $1 =~ ^/ ]]; then
                    dir=$1;
                else
                    local wdLower=$(echo -n "$PWD" | tr '[:upper:]' '[:lower:]');
                    local tokenLower=$(echo -n "$1" | tr '[:upper:]' '[:lower:]');
                    local newParentDirLower=${wdLower%/$tokenLower/*};
                    [[ "$newParentDirLower" == "$wdLower" ]] && { 
                        echo "$FUNCNAME: No ancestral directory named '$1' found." 1>&2;
                        return 1
                    };
                    local targetDirPathLength=$(( ${#newParentDirLower} + 1 + ${#tokenLower} ));
                    dir=${PWD:0:$targetDirPathLength};
                fi;
            fi;
        fi;
        pushd "$dir" > /dev/null
    }

我同意mklement0,这应该是一个函数。但是更简单的一个

将此添加到您的
.bashrc

cdd(){
newDir=“${PWD%%$1*}$1”
如果[!-d“$newDir”];则
echo“cdd:$1:没有这样的文件或目录”>&2
返回1
fi
cd“${newDir}”
}
请注意,如果路径中多次出现
$1
(您的搜索字符串),此函数将首选第一个。还请注意,如果
$1
是路径的子字符串,则不会找到它。例如:

[ghoti@pc~]$mkdir-p/tmp/foo/bar/baz/foo/one
[ghoti@pc~]$cd/tmp/foo/bar/baz/foo/one
[ghoti@pc/tmp/foo/bar/baz/foo/one]$cdd foo
[ghoti@pc/tmp/foo]$cd-
/tmp/foo/bar/baz/foo/one
[ghoti@pc/tmp/foo/bar/baz/foo/one]$cdd fo
cdd:fo:没有这样的文件或目录
如果您想通过运行
cdd2
来增加两个级别的功能,这可能会起作用:

cdd () {
  newDir="${PWD%%$1*}$1"
  if [ "$1" -gt 0 -a "$1" = "${1%%.*}" -a ! -d "$1" ]; then
    newDir=""
    for _ in $(seq 1 $1); do
      newDir="../${newDir}"
    done
    cd $newDir
    return 0
  elif [ ! -d "$newDir" ]; then
    echo "cdd: $1: No such file or directory" >&2
    return 1
  fi
  cd "${newDir}"
}

if
语句验证您提供的整数本身不是目录。我们构建了一个新的
$newDir
,这样您就可以
cd-
返回原始位置,如果您愿意的话。

我同意mklement0,这应该是一个函数。但是更简单的一个

将此添加到您的
.bashrc

cdd(){
newDir=“${PWD%%$1*}$1”
如果[!-d“$newDir”];则
echo“cdd:$1:没有这样的文件或目录”>&2
返回1
fi
cd“${newDir}”
}
请注意,如果路径中多次出现
$1
(您的搜索字符串),此函数将首选第一个。还请注意,如果
$1
是路径的子字符串,则不会找到它。例如:

[ghoti@pc~]$mkdir-p/tmp/foo/bar/baz/foo/one
[ghoti@pc~]$cd/tmp/foo/bar/baz/foo/one
[ghoti@pc/tmp/foo/bar/baz/foo/one]$cdd foo
[ghoti@pc/tmp/foo]$cd-
/tmp/foo/bar/baz/foo/one
[ghoti@pc/tmp/foo/bar/baz/foo/one]$cdd fo
cdd:fo:没有这样的文件或目录
如果您想通过运行
cdd2
来增加两个级别的功能,这可能会起作用:

cdd () {
  newDir="${PWD%%$1*}$1"
  if [ "$1" -gt 0 -a "$1" = "${1%%.*}" -a ! -d "$1" ]; then
    newDir=""
    for _ in $(seq 1 $1); do
      newDir="../${newDir}"
    done
    cd $newDir
    return 0
  elif [ ! -d "$newDir" ]; then
    echo "cdd: $1: No such file or directory" >&2
    return 1
  fi
  cd "${newDir}"
}

if
语句验证您提供的整数本身不是目录。我们构建了一个新的
$newDir
,这样您就可以
cd-
回到您的原始位置,如果您愿意的话。

这是一个稍微简单的函数,我认为它实现了您想要做的:

cdd() { cd ${PWD/$1*}$1; }
说明:

${PWD/$1*}$1
获取当前工作目录,并在传递给它的字符串(目标目录)后去除所有内容,然后将该字符串添加回。然后将其用作
cd
的参数。我没有添加任何错误处理,因为
cd
将自行处理

例如:

[atticus:pgl]:~/tmp/a/b/c/d/e/f $ cdd b
[atticus:pgl]:~/tmp/a/b $

这有点难看,但它可以工作。

这是一个稍微简单的函数,我认为它可以实现您想要实现的功能:

cdd() { cd ${PWD/$1*}$1; }
说明:

${PWD/$1*}$1
获取当前工作目录,并在传递给它的字符串(目标目录)后去除所有内容,然后将该字符串添加回。然后将其用作
cd
的参数。我没有添加任何错误处理,因为
cd
将自行处理

例如:

[atticus:pgl]:~/tmp/a/b/c/d/e/f $ cdd b
[atticus:pgl]:~/tmp/a/b $

虽然有点难看,但它确实有效。

我认为您需要编写一个脚本来完成此操作。。。你可能会想出一个
xargs
hack。我想你需要写一个脚本来完成这个。。。您可能会想出一个
xargs
hack。