Unix 终端,“cd .."对于特定数量的目录 [英] Unix terminal, "cd .." for a specific number of directories
本文介绍了Unix 终端,“cd .."对于特定数量的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有这样的目录结构:
Let's suppose we have this directory structure:
/home/myuser/dir_1/sub_1/sub_2/sub_3
而我想从sub_3遍历到dir_1,我需要做的是
and I want to traverse from sub_3 to dir_1, what I need to do is
cd ../../..
我的问题是,没有更短的吗?我的意思是:
My question is, isn't there something shorter? I mean something like:
cd -t 3
你可以在哪里告诉 shell 你想要返回多少个目录.
where you can tell the shell how many directories you want to go back.
推荐答案
使用 printf
然后 cd
构建路径:
Build the path using printf
then cd
to it:
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
具有 cd
被调用一次的良好属性,无论 N 有多大.
Has the nice property that cd
is called once, regardless of how big N is.
这篇关于Unix 终端,“cd .."对于特定数量的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文