shell – Unix终端,“cd ..”表示特定数量的目录

假设我们有这个目录结构:

/home/myuser/dir_1/sub_1/sub_2/sub_3

我想从sub_3遍历到dir_1,我需要做的是

cd ../../..

我的问题是,是不是有更短的东西?
我的意思是:

cd -t 3

在哪里可以告诉shell你想要返回多少个目录.

解决方法

使用printf构建路径然后cd到它:

cdup() {
    # $1=number of times,defaults to 1
    local path
    printf -v path '%*s' "${1:-1}"
    cd "${path// /../}"
}

用于:

cdup 4 # to go up four directories
cdup 1 # to explicitly go up one directory
cdup   # to implicitly go up one

不管N有多大,都有一次调用cd的好属性.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...