在linux中上几个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12198222/
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
Go up several directories in linux
提问by user15683854875644328975643872
When I want to go one directory up I use
当我想向上移动一个目录时,我使用
cd ..
cd ..
But when I want to go 7 directories up is there any way to do this other than just typing seven (7) times cd ..
?
但是,当我想要向上 7 个目录时,除了键入七 (7) 次之外,还有其他方法可以做到这一点cd ..
吗?
采纳答案by Florin Stingaciu
cd ../../../../../../../
Also another useful navigation tip is if for example lets say you keep switching from a directory (call it A) to another (call it B) that's 7 directories up, in your case.
另一个有用的导航提示是,例如,假设您一直从一个目录(称为 A)切换到另一个(称为 B),在您的情况下,它有 7 个目录。
So if you're in directory A:
所以如果你在目录 A 中:
A> cd ../../../../../../../
B> // Now you're in directory B and want to go back to A
B> cd -
That will move right back to directory A. -
expands to the previous directory you were in.
这将返回到目录 A。-
扩展到您所在的上一个目录。
回答by Maksim Skurydzin
You can do it like this cd ../../../../../../../
你可以这样做 cd ../../../../../../../
There is a cool articleabout some hacks you can apply to improve you navigation.
有一篇很酷的文章介绍了一些可用于改进导航的技巧。
回答by E. Lüders
Hm, I don't think so.
嗯,我不这么认为。
But you can write cd ../../../..
(and so on) instead of 7 times cd ..
但是你可以写cd ../../../..
(等等)而不是 7 次 cd ..
回答by Waleed Khan
for i in {1..7}; do cd ..; done
回答by Chakalaka
you can use pushd .
to remember one directory and popd
to go back to it.
您可以使用pushd .
记住一个目录并popd
返回到它。
回答by Daniel Thaagaard Andreasen
If there is a command I use a lot I will just make an alias.
如果有一个我经常使用的命令,我只会创建一个别名。
You could type
你可以输入
alias ..='cd ..'
alias ...='cd ../..'
Then you can just use ..
to go one level up and ...
to go two levels up.
然后你可以只使用..
向上一级和...
向上两级。
回答by RussellStewart
Here is a slight improvement I have found:
这是我发现的一个小改进:
Usually, when you go back one directory with cd .., you end up going forward one directory after. To make this work, you have to use functions rather than aliases, so instead of:
通常,当您使用 cd .. 返回一个目录时,您最终会在之后前进一个目录。为了使这项工作,您必须使用函数而不是别名,而不是:
alias ..=”cd ../”
alias ..2=”cd ../../”
you can use:
您可以使用:
..()
{
cd ../$@
}
..2()
{
cd ../../$@
}
However, after using this, it quickly becomes apparent that you are missing command completion for this function, making it far less useful than it could be. Thus, I went back and added my own bash completion functions for this function, which can be pasted into your ~/.bashrc or any file (e.g. ~/.bash_completion) that is called from your ~/.bashrc if you wish to avoid clutter. Here is the completion code:
但是,在使用此功能后,您很快就会发现您缺少此功能的命令完成功能,从而使其变得不那么有用。因此,我回过头来为这个函数添加了我自己的 bash 完成函数,如果你想避免,它可以粘贴到你的 ~/.bashrc 或从你的 ~/.bashrc 调用的任何文件(例如 ~/.bash_completion)中杂乱。这是完成代码:
_..()
{
local cur=../${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -d — $cur) )
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=$(echo ${COMPREPLY[$i]} | sed -r ‘s/(\.\.\/)*//')
done
}
complete -F _.. -o nospace -S / ..
_..2()
{
local cur=../../${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -d — $cur) )
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=$(echo ${COMPREPLY[$i]} | sed -r ‘s/(\.\.\/)*//')
done
}
complete -F _..2 -o nospace -S / ..2
回答by Grigory K
Make alias (in you ~/.bashrc)
制作别名(在你 ~/.bashrc 中)
function cd_up() {
cd $(printf "%0.0s../" $(seq 1 ));
}
alias 'cd..'='cd_up'
and use:
并使用:
$ cd.. 7
UPD:Or make more powerfull variant, cd to dir name in current path:
UPD:或者制作更强大的变体,cd 到当前路径中的目录名称:
# cd up to n dirs
# using: cd.. 10 cd.. dir
function cd_up() {
case in
*[!0-9]*) # if no a number
cd $( pwd | sed -r "s|(.*/[^/]*/).*||" ) # search dir_name in current path, if found - cd to it
;; # if not found - not cd
*)
cd $(printf "%0.0s../" $(seq 1 )); # cd ../../../../ (N dirs)
;;
esac
}
alias 'cd..'='cd_up' # can not name function 'cd..'
use:
用:
$ cd /home/user/documents/projects/reports/2014-10-01
$ cd.. doc
$ pwd
> /home/user/documents
回答by Blow-Out
You may try this solution. It is work fine for me:
你可以试试这个解决方案。这对我来说很好用:
#!/bin/bash
#this sctipt should take one parameter - number of steps. And do "cd" command step times.
# example usage: cd `cde 3`
NEW_PATH=$(pwd)
step="../"
upper=""
for i in seq
do
upper="$upper$step"
done
echo $upper
I have made alias in ~/.barsh file. And this work fine for me.
Example, up for three folders.
$cd `cde 3`
我在 ~/.barsh 文件中创建了别名。这对我来说很好用。
例如,最多三个文件夹。
$cd `cde 3`
回答by doberoi96
You could add the following function to your .bashrc
:
您可以将以下功能添加到您的.bashrc
:
# ~/.bashrc
up() {
for D in $(seq 1 );
do
cd ..
done
}
So the usage would simply be:
所以用法很简单:
user ~ $ up 3
user / $