Using cd to go up multiple directory levels
I'm dealing with java projects which often result in deeply nested folders (/path/to/project/com/java/lang/whatever, etc) and sometimes want to be able to jump, say, 4 directory levels upwards. Typing cd ../../../.. is a pain, and I don't want to symlink. Is there some flag to cd that lets you go up multiple directory levels (in my head, it would be something like cd -u 4)? Unfortunately I can't find any man page for cd specifically, instead just getting the useless "builtins" page.
linux command-line
add a comment |
I'm dealing with java projects which often result in deeply nested folders (/path/to/project/com/java/lang/whatever, etc) and sometimes want to be able to jump, say, 4 directory levels upwards. Typing cd ../../../.. is a pain, and I don't want to symlink. Is there some flag to cd that lets you go up multiple directory levels (in my head, it would be something like cd -u 4)? Unfortunately I can't find any man page for cd specifically, instead just getting the useless "builtins" page.
linux command-line
Modifying bash to interpretcd ...
where the number of '.' would be the number of levels to go up. Would this cause a conflict that I'm not aware of?
– user48420
Apr 19 '16 at 17:48
add a comment |
I'm dealing with java projects which often result in deeply nested folders (/path/to/project/com/java/lang/whatever, etc) and sometimes want to be able to jump, say, 4 directory levels upwards. Typing cd ../../../.. is a pain, and I don't want to symlink. Is there some flag to cd that lets you go up multiple directory levels (in my head, it would be something like cd -u 4)? Unfortunately I can't find any man page for cd specifically, instead just getting the useless "builtins" page.
linux command-line
I'm dealing with java projects which often result in deeply nested folders (/path/to/project/com/java/lang/whatever, etc) and sometimes want to be able to jump, say, 4 directory levels upwards. Typing cd ../../../.. is a pain, and I don't want to symlink. Is there some flag to cd that lets you go up multiple directory levels (in my head, it would be something like cd -u 4)? Unfortunately I can't find any man page for cd specifically, instead just getting the useless "builtins" page.
linux command-line
linux command-line
asked Jul 16 '12 at 19:36
Tossrock
146123
146123
Modifying bash to interpretcd ...
where the number of '.' would be the number of levels to go up. Would this cause a conflict that I'm not aware of?
– user48420
Apr 19 '16 at 17:48
add a comment |
Modifying bash to interpretcd ...
where the number of '.' would be the number of levels to go up. Would this cause a conflict that I'm not aware of?
– user48420
Apr 19 '16 at 17:48
Modifying bash to interpret
cd ...
where the number of '.' would be the number of levels to go up. Would this cause a conflict that I'm not aware of?– user48420
Apr 19 '16 at 17:48
Modifying bash to interpret
cd ...
where the number of '.' would be the number of levels to go up. Would this cause a conflict that I'm not aware of?– user48420
Apr 19 '16 at 17:48
add a comment |
12 Answers
12
active
oldest
votes
Or... try this: (yay Google)
Navigate up the directory using ..n :
In the example below, ..4 is used to go up 4 directory level, ..3 to
go up 3 directory level, ..2 to go up 2 directory level.
Add the following alias to the .bash_profile and re-login.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
(etc)
See Hack #2
10
I would rather use the aliases..
,...
and....
as they are faster to type, but the principle of aliases is the same of course.
– Bernhard
Jul 16 '12 at 20:24
1
@bernhard You're more than welcome to, as I don't believe it would cause any conflicts. I just left it as-is because I quoted it from the website.
– cutrightjm
Jul 16 '12 at 20:25
The 4DOS.EXE shell for DOS also supportedcd ...
,cd ....
etc. It was very handy.
– Alexios
Jul 25 '13 at 21:43
add a comment |
A simple function, with an alias, too:
function cd_up() {
cd $(printf "%0.s../" $(seq 1 $1 ));
}
alias 'cd..'='cd_up'
(You could define this in ~/.bashrc
if you want it in every instance).
It's simple to use:
$ cd.. 10
Nice one! Works for me! Bash 4.3.11
– Carlos Nunez
Sep 18 '15 at 18:29
1
Good answer; you might want to point out that this is better than solutions such aseval $(printf "cd ..;%.s" $(seq $1))
whichcd
multiple times, because~-
aka$OLDPWD
is set to the working directory from before invocation, rather than to an intermediate directory. P.S. - it simplifies tocd $(printf %.s../ $(seq "$1"))
if you want.
– Toby Speight
Mar 15 '17 at 13:45
add a comment |
Turns out the correct answer is 'cd +n', where n is the number of levels you want to go up. Too bad this isn't documented anywhere!
14
at least it does NOT work in bash.
– HongboZhu
Mar 26 '13 at 15:32
5
Doesn't work on bash 4.2.25 as shipped with Debian. Out of curiosity, what *nix and/or shell are you using?
– Alexios
Jul 25 '13 at 21:44
Bash has a built-in calleddirs
that takes [+n] as an argument, and prints the nth unique directory that was added viapushd
. If youalias
cd
topushd
, then you can use this. However, note that this is not technically an answer to the OP's question, because this has to do with unique directories, meaning that the order gets messed up over time, as you return to the same directories.
– Brian Peterson
Oct 15 '13 at 18:07
Yeah,pushd
andpopd
also take a [+n] option, which is why that is able to work. Also, though it's imperfect and indirect, I guess this can be used as an answer to the question. Often you visited the directories above you recently, so they will be in the last 10 dirs stored by the directory stack.
– Brian Peterson
Oct 15 '13 at 18:15
did not work for me...
– Vass
Sep 23 '16 at 0:31
add a comment |
A simple, low-tech solution that doesn't need any setup. Only works in shells with bash
-style command editing, though.
- Type
cd ..
- Press Up-arrow Return as many times as needed. Very fast if you use two fingers.
2
haha, not a bad answer i guess :)
– joshmcode
Apr 6 '18 at 19:52
add a comment |
You could write a function (it has to be a function, as you want to change the state of your shell itself, namely the working directory; an external command would affect only its own process).
Here's a function that will go up a number of levels passed as argument (default 1) in the physical directory structure (so, like cd -P ..
, n times):
up() {
# default parameter to 1 if non provided
declare -i d=${@:-1}
# ensure given parameter is non-negative. Print error and return if it is
(( $d < 0 )) && (>&2 echo "up: Error: negative value provided") && return 1;
# remove last d directories from pwd, append "/" in case result is empty
cd "$(pwd | sed -E 's;(/[^/]*){0,'$d'}$;;')/";
}
Use it like this:
up 4
1
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– Donald Duck
Mar 13 '17 at 16:01
1
Agreed, I have added a few comments to help.
– mgild
Mar 13 '17 at 19:11
To be nitpicky, isn'tup 4
really equivalent tocd /some/dir/
assuming$PWD = "/some/dir/that/is/deeply/nested/"
?
– snapfractalpop
Aug 15 '18 at 23:28
add a comment |
Not exactly what you're asking for but you should look into pushd
and popd
. I find them much more useful for folder navigation than some cd...
alias
If you're going back and forth from a couple fixed areas, the usual thing is to have aliases.
alias proj1='cd /some/dir/containing/proj1'
alias proj2='cd /some/deeper/dir/structure/containing/proj2'
add a comment |
Instead of using aliases you could also use the following bash function:
function mcd() {
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
(or as a one-liner: function mcd() { up=""; for ((i=1; i<=$1;i++)); do up="${up}../"; done; cd $up; }
)
Adding this to your ~/.bashrc
file will make it available in your terminal and the building of a String ../../../../../../
before calling cd
will also make it possible to use cd -
to jump back to the start directory.
A more helpful implementation could also contain some user-input checks:
function mcd() {
if [[ $1 -lt 1 ]]; then
echo "Only positive integer values larger than 1 are allowed!" >&2
echo -e "ntUsage:nt======nntt# to go up 10 levels in your directorynttmcd 10nntt# to go up just 2 levelsnttmcd 2n" >&2
return 1;
fi
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
add a comment |
Do you know about autojump? It's a third party hack, but can be useful in your scenario.
add a comment |
Try the rarely used environment parameter CDPATH
. Then you might not have to explicitly set the level.
Example:
$ find workspace -type d
workspace
workspace/project1
workspace/project1/com
workspace/project1/com/java
workspace/project1/com/java/lang
$ CDPATH=".:~/workspace:~/workspace/project1:~/workspace/project1/com:~/workspace/project1/com/java:~/workspace/project1/com/java/lang"
$ cd com
$ pwd
~/workspace/project1/com
If working on multiple projects, you can make the CDPATH setting into a project specific environment file. And trigger it with a shim for additional automation.
I tend to use pushd and popd quite a lot. I tend to use CDPATH to let me hop between project trees rather than subdirs in a project - but at the moment I'm working on a lot of small projects, not a few big projects. :)
add a comment |
If you are previously on the target directory:
luna:/tmp % mkdir -p a/b/c/d
luna:/tmp % pwd
/tmp
luna:/tmp % cd a/b/c/d
luna:d % pwd
/tmp/a/b/c/d
luna:d % cd -
luna:/tmp % pwd
/tmp
luna:/tmp %
add a comment |
Take a look at DirB. It's a BASH script that allows you to create bookmarks as follows:
OPERATIONS
- s Save a directory bookmark
- g go to a bookmark or named directory
- p push a bookmark/directory onto the dir stack
- r remove saved bookmark
- d display bookmarked directory path
- sl print the list of directory bookmarks
Very simple, very effective.
add a comment |
Step further from @Grigory's answer:
function cd_up() {
if [ -z "$1" ]; then
cd ..
else
cd $(printf "%0.s../" $(seq 1 $1 ))
fi
}
alias 'cdd'='cd_up'
That is :
// go up 1 directory level
$cdd
// go up 3 directory level
$cdd 3
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f449687%2fusing-cd-to-go-up-multiple-directory-levels%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
Or... try this: (yay Google)
Navigate up the directory using ..n :
In the example below, ..4 is used to go up 4 directory level, ..3 to
go up 3 directory level, ..2 to go up 2 directory level.
Add the following alias to the .bash_profile and re-login.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
(etc)
See Hack #2
10
I would rather use the aliases..
,...
and....
as they are faster to type, but the principle of aliases is the same of course.
– Bernhard
Jul 16 '12 at 20:24
1
@bernhard You're more than welcome to, as I don't believe it would cause any conflicts. I just left it as-is because I quoted it from the website.
– cutrightjm
Jul 16 '12 at 20:25
The 4DOS.EXE shell for DOS also supportedcd ...
,cd ....
etc. It was very handy.
– Alexios
Jul 25 '13 at 21:43
add a comment |
Or... try this: (yay Google)
Navigate up the directory using ..n :
In the example below, ..4 is used to go up 4 directory level, ..3 to
go up 3 directory level, ..2 to go up 2 directory level.
Add the following alias to the .bash_profile and re-login.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
(etc)
See Hack #2
10
I would rather use the aliases..
,...
and....
as they are faster to type, but the principle of aliases is the same of course.
– Bernhard
Jul 16 '12 at 20:24
1
@bernhard You're more than welcome to, as I don't believe it would cause any conflicts. I just left it as-is because I quoted it from the website.
– cutrightjm
Jul 16 '12 at 20:25
The 4DOS.EXE shell for DOS also supportedcd ...
,cd ....
etc. It was very handy.
– Alexios
Jul 25 '13 at 21:43
add a comment |
Or... try this: (yay Google)
Navigate up the directory using ..n :
In the example below, ..4 is used to go up 4 directory level, ..3 to
go up 3 directory level, ..2 to go up 2 directory level.
Add the following alias to the .bash_profile and re-login.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
(etc)
See Hack #2
Or... try this: (yay Google)
Navigate up the directory using ..n :
In the example below, ..4 is used to go up 4 directory level, ..3 to
go up 3 directory level, ..2 to go up 2 directory level.
Add the following alias to the .bash_profile and re-login.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
(etc)
See Hack #2
answered Jul 16 '12 at 20:20
cutrightjm
3,35332047
3,35332047
10
I would rather use the aliases..
,...
and....
as they are faster to type, but the principle of aliases is the same of course.
– Bernhard
Jul 16 '12 at 20:24
1
@bernhard You're more than welcome to, as I don't believe it would cause any conflicts. I just left it as-is because I quoted it from the website.
– cutrightjm
Jul 16 '12 at 20:25
The 4DOS.EXE shell for DOS also supportedcd ...
,cd ....
etc. It was very handy.
– Alexios
Jul 25 '13 at 21:43
add a comment |
10
I would rather use the aliases..
,...
and....
as they are faster to type, but the principle of aliases is the same of course.
– Bernhard
Jul 16 '12 at 20:24
1
@bernhard You're more than welcome to, as I don't believe it would cause any conflicts. I just left it as-is because I quoted it from the website.
– cutrightjm
Jul 16 '12 at 20:25
The 4DOS.EXE shell for DOS also supportedcd ...
,cd ....
etc. It was very handy.
– Alexios
Jul 25 '13 at 21:43
10
10
I would rather use the aliases
..
, ...
and ....
as they are faster to type, but the principle of aliases is the same of course.– Bernhard
Jul 16 '12 at 20:24
I would rather use the aliases
..
, ...
and ....
as they are faster to type, but the principle of aliases is the same of course.– Bernhard
Jul 16 '12 at 20:24
1
1
@bernhard You're more than welcome to, as I don't believe it would cause any conflicts. I just left it as-is because I quoted it from the website.
– cutrightjm
Jul 16 '12 at 20:25
@bernhard You're more than welcome to, as I don't believe it would cause any conflicts. I just left it as-is because I quoted it from the website.
– cutrightjm
Jul 16 '12 at 20:25
The 4DOS.EXE shell for DOS also supported
cd ...
, cd ....
etc. It was very handy.– Alexios
Jul 25 '13 at 21:43
The 4DOS.EXE shell for DOS also supported
cd ...
, cd ....
etc. It was very handy.– Alexios
Jul 25 '13 at 21:43
add a comment |
A simple function, with an alias, too:
function cd_up() {
cd $(printf "%0.s../" $(seq 1 $1 ));
}
alias 'cd..'='cd_up'
(You could define this in ~/.bashrc
if you want it in every instance).
It's simple to use:
$ cd.. 10
Nice one! Works for me! Bash 4.3.11
– Carlos Nunez
Sep 18 '15 at 18:29
1
Good answer; you might want to point out that this is better than solutions such aseval $(printf "cd ..;%.s" $(seq $1))
whichcd
multiple times, because~-
aka$OLDPWD
is set to the working directory from before invocation, rather than to an intermediate directory. P.S. - it simplifies tocd $(printf %.s../ $(seq "$1"))
if you want.
– Toby Speight
Mar 15 '17 at 13:45
add a comment |
A simple function, with an alias, too:
function cd_up() {
cd $(printf "%0.s../" $(seq 1 $1 ));
}
alias 'cd..'='cd_up'
(You could define this in ~/.bashrc
if you want it in every instance).
It's simple to use:
$ cd.. 10
Nice one! Works for me! Bash 4.3.11
– Carlos Nunez
Sep 18 '15 at 18:29
1
Good answer; you might want to point out that this is better than solutions such aseval $(printf "cd ..;%.s" $(seq $1))
whichcd
multiple times, because~-
aka$OLDPWD
is set to the working directory from before invocation, rather than to an intermediate directory. P.S. - it simplifies tocd $(printf %.s../ $(seq "$1"))
if you want.
– Toby Speight
Mar 15 '17 at 13:45
add a comment |
A simple function, with an alias, too:
function cd_up() {
cd $(printf "%0.s../" $(seq 1 $1 ));
}
alias 'cd..'='cd_up'
(You could define this in ~/.bashrc
if you want it in every instance).
It's simple to use:
$ cd.. 10
A simple function, with an alias, too:
function cd_up() {
cd $(printf "%0.s../" $(seq 1 $1 ));
}
alias 'cd..'='cd_up'
(You could define this in ~/.bashrc
if you want it in every instance).
It's simple to use:
$ cd.. 10
edited Mar 15 '17 at 13:31
Toby Speight
3,6061532
3,6061532
answered Oct 1 '14 at 5:31
Grigory K
29122
29122
Nice one! Works for me! Bash 4.3.11
– Carlos Nunez
Sep 18 '15 at 18:29
1
Good answer; you might want to point out that this is better than solutions such aseval $(printf "cd ..;%.s" $(seq $1))
whichcd
multiple times, because~-
aka$OLDPWD
is set to the working directory from before invocation, rather than to an intermediate directory. P.S. - it simplifies tocd $(printf %.s../ $(seq "$1"))
if you want.
– Toby Speight
Mar 15 '17 at 13:45
add a comment |
Nice one! Works for me! Bash 4.3.11
– Carlos Nunez
Sep 18 '15 at 18:29
1
Good answer; you might want to point out that this is better than solutions such aseval $(printf "cd ..;%.s" $(seq $1))
whichcd
multiple times, because~-
aka$OLDPWD
is set to the working directory from before invocation, rather than to an intermediate directory. P.S. - it simplifies tocd $(printf %.s../ $(seq "$1"))
if you want.
– Toby Speight
Mar 15 '17 at 13:45
Nice one! Works for me! Bash 4.3.11
– Carlos Nunez
Sep 18 '15 at 18:29
Nice one! Works for me! Bash 4.3.11
– Carlos Nunez
Sep 18 '15 at 18:29
1
1
Good answer; you might want to point out that this is better than solutions such as
eval $(printf "cd ..;%.s" $(seq $1))
which cd
multiple times, because ~-
aka $OLDPWD
is set to the working directory from before invocation, rather than to an intermediate directory. P.S. - it simplifies to cd $(printf %.s../ $(seq "$1"))
if you want.– Toby Speight
Mar 15 '17 at 13:45
Good answer; you might want to point out that this is better than solutions such as
eval $(printf "cd ..;%.s" $(seq $1))
which cd
multiple times, because ~-
aka $OLDPWD
is set to the working directory from before invocation, rather than to an intermediate directory. P.S. - it simplifies to cd $(printf %.s../ $(seq "$1"))
if you want.– Toby Speight
Mar 15 '17 at 13:45
add a comment |
Turns out the correct answer is 'cd +n', where n is the number of levels you want to go up. Too bad this isn't documented anywhere!
14
at least it does NOT work in bash.
– HongboZhu
Mar 26 '13 at 15:32
5
Doesn't work on bash 4.2.25 as shipped with Debian. Out of curiosity, what *nix and/or shell are you using?
– Alexios
Jul 25 '13 at 21:44
Bash has a built-in calleddirs
that takes [+n] as an argument, and prints the nth unique directory that was added viapushd
. If youalias
cd
topushd
, then you can use this. However, note that this is not technically an answer to the OP's question, because this has to do with unique directories, meaning that the order gets messed up over time, as you return to the same directories.
– Brian Peterson
Oct 15 '13 at 18:07
Yeah,pushd
andpopd
also take a [+n] option, which is why that is able to work. Also, though it's imperfect and indirect, I guess this can be used as an answer to the question. Often you visited the directories above you recently, so they will be in the last 10 dirs stored by the directory stack.
– Brian Peterson
Oct 15 '13 at 18:15
did not work for me...
– Vass
Sep 23 '16 at 0:31
add a comment |
Turns out the correct answer is 'cd +n', where n is the number of levels you want to go up. Too bad this isn't documented anywhere!
14
at least it does NOT work in bash.
– HongboZhu
Mar 26 '13 at 15:32
5
Doesn't work on bash 4.2.25 as shipped with Debian. Out of curiosity, what *nix and/or shell are you using?
– Alexios
Jul 25 '13 at 21:44
Bash has a built-in calleddirs
that takes [+n] as an argument, and prints the nth unique directory that was added viapushd
. If youalias
cd
topushd
, then you can use this. However, note that this is not technically an answer to the OP's question, because this has to do with unique directories, meaning that the order gets messed up over time, as you return to the same directories.
– Brian Peterson
Oct 15 '13 at 18:07
Yeah,pushd
andpopd
also take a [+n] option, which is why that is able to work. Also, though it's imperfect and indirect, I guess this can be used as an answer to the question. Often you visited the directories above you recently, so they will be in the last 10 dirs stored by the directory stack.
– Brian Peterson
Oct 15 '13 at 18:15
did not work for me...
– Vass
Sep 23 '16 at 0:31
add a comment |
Turns out the correct answer is 'cd +n', where n is the number of levels you want to go up. Too bad this isn't documented anywhere!
Turns out the correct answer is 'cd +n', where n is the number of levels you want to go up. Too bad this isn't documented anywhere!
answered Oct 25 '12 at 21:31
Tossrock
19712
19712
14
at least it does NOT work in bash.
– HongboZhu
Mar 26 '13 at 15:32
5
Doesn't work on bash 4.2.25 as shipped with Debian. Out of curiosity, what *nix and/or shell are you using?
– Alexios
Jul 25 '13 at 21:44
Bash has a built-in calleddirs
that takes [+n] as an argument, and prints the nth unique directory that was added viapushd
. If youalias
cd
topushd
, then you can use this. However, note that this is not technically an answer to the OP's question, because this has to do with unique directories, meaning that the order gets messed up over time, as you return to the same directories.
– Brian Peterson
Oct 15 '13 at 18:07
Yeah,pushd
andpopd
also take a [+n] option, which is why that is able to work. Also, though it's imperfect and indirect, I guess this can be used as an answer to the question. Often you visited the directories above you recently, so they will be in the last 10 dirs stored by the directory stack.
– Brian Peterson
Oct 15 '13 at 18:15
did not work for me...
– Vass
Sep 23 '16 at 0:31
add a comment |
14
at least it does NOT work in bash.
– HongboZhu
Mar 26 '13 at 15:32
5
Doesn't work on bash 4.2.25 as shipped with Debian. Out of curiosity, what *nix and/or shell are you using?
– Alexios
Jul 25 '13 at 21:44
Bash has a built-in calleddirs
that takes [+n] as an argument, and prints the nth unique directory that was added viapushd
. If youalias
cd
topushd
, then you can use this. However, note that this is not technically an answer to the OP's question, because this has to do with unique directories, meaning that the order gets messed up over time, as you return to the same directories.
– Brian Peterson
Oct 15 '13 at 18:07
Yeah,pushd
andpopd
also take a [+n] option, which is why that is able to work. Also, though it's imperfect and indirect, I guess this can be used as an answer to the question. Often you visited the directories above you recently, so they will be in the last 10 dirs stored by the directory stack.
– Brian Peterson
Oct 15 '13 at 18:15
did not work for me...
– Vass
Sep 23 '16 at 0:31
14
14
at least it does NOT work in bash.
– HongboZhu
Mar 26 '13 at 15:32
at least it does NOT work in bash.
– HongboZhu
Mar 26 '13 at 15:32
5
5
Doesn't work on bash 4.2.25 as shipped with Debian. Out of curiosity, what *nix and/or shell are you using?
– Alexios
Jul 25 '13 at 21:44
Doesn't work on bash 4.2.25 as shipped with Debian. Out of curiosity, what *nix and/or shell are you using?
– Alexios
Jul 25 '13 at 21:44
Bash has a built-in called
dirs
that takes [+n] as an argument, and prints the nth unique directory that was added via pushd
. If you alias
cd
to pushd
, then you can use this. However, note that this is not technically an answer to the OP's question, because this has to do with unique directories, meaning that the order gets messed up over time, as you return to the same directories.– Brian Peterson
Oct 15 '13 at 18:07
Bash has a built-in called
dirs
that takes [+n] as an argument, and prints the nth unique directory that was added via pushd
. If you alias
cd
to pushd
, then you can use this. However, note that this is not technically an answer to the OP's question, because this has to do with unique directories, meaning that the order gets messed up over time, as you return to the same directories.– Brian Peterson
Oct 15 '13 at 18:07
Yeah,
pushd
and popd
also take a [+n] option, which is why that is able to work. Also, though it's imperfect and indirect, I guess this can be used as an answer to the question. Often you visited the directories above you recently, so they will be in the last 10 dirs stored by the directory stack.– Brian Peterson
Oct 15 '13 at 18:15
Yeah,
pushd
and popd
also take a [+n] option, which is why that is able to work. Also, though it's imperfect and indirect, I guess this can be used as an answer to the question. Often you visited the directories above you recently, so they will be in the last 10 dirs stored by the directory stack.– Brian Peterson
Oct 15 '13 at 18:15
did not work for me...
– Vass
Sep 23 '16 at 0:31
did not work for me...
– Vass
Sep 23 '16 at 0:31
add a comment |
A simple, low-tech solution that doesn't need any setup. Only works in shells with bash
-style command editing, though.
- Type
cd ..
- Press Up-arrow Return as many times as needed. Very fast if you use two fingers.
2
haha, not a bad answer i guess :)
– joshmcode
Apr 6 '18 at 19:52
add a comment |
A simple, low-tech solution that doesn't need any setup. Only works in shells with bash
-style command editing, though.
- Type
cd ..
- Press Up-arrow Return as many times as needed. Very fast if you use two fingers.
2
haha, not a bad answer i guess :)
– joshmcode
Apr 6 '18 at 19:52
add a comment |
A simple, low-tech solution that doesn't need any setup. Only works in shells with bash
-style command editing, though.
- Type
cd ..
- Press Up-arrow Return as many times as needed. Very fast if you use two fingers.
A simple, low-tech solution that doesn't need any setup. Only works in shells with bash
-style command editing, though.
- Type
cd ..
- Press Up-arrow Return as many times as needed. Very fast if you use two fingers.
edited Nov 20 '15 at 15:14
answered Nov 20 '15 at 14:40
Tom Zych
848616
848616
2
haha, not a bad answer i guess :)
– joshmcode
Apr 6 '18 at 19:52
add a comment |
2
haha, not a bad answer i guess :)
– joshmcode
Apr 6 '18 at 19:52
2
2
haha, not a bad answer i guess :)
– joshmcode
Apr 6 '18 at 19:52
haha, not a bad answer i guess :)
– joshmcode
Apr 6 '18 at 19:52
add a comment |
You could write a function (it has to be a function, as you want to change the state of your shell itself, namely the working directory; an external command would affect only its own process).
Here's a function that will go up a number of levels passed as argument (default 1) in the physical directory structure (so, like cd -P ..
, n times):
up() {
# default parameter to 1 if non provided
declare -i d=${@:-1}
# ensure given parameter is non-negative. Print error and return if it is
(( $d < 0 )) && (>&2 echo "up: Error: negative value provided") && return 1;
# remove last d directories from pwd, append "/" in case result is empty
cd "$(pwd | sed -E 's;(/[^/]*){0,'$d'}$;;')/";
}
Use it like this:
up 4
1
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– Donald Duck
Mar 13 '17 at 16:01
1
Agreed, I have added a few comments to help.
– mgild
Mar 13 '17 at 19:11
To be nitpicky, isn'tup 4
really equivalent tocd /some/dir/
assuming$PWD = "/some/dir/that/is/deeply/nested/"
?
– snapfractalpop
Aug 15 '18 at 23:28
add a comment |
You could write a function (it has to be a function, as you want to change the state of your shell itself, namely the working directory; an external command would affect only its own process).
Here's a function that will go up a number of levels passed as argument (default 1) in the physical directory structure (so, like cd -P ..
, n times):
up() {
# default parameter to 1 if non provided
declare -i d=${@:-1}
# ensure given parameter is non-negative. Print error and return if it is
(( $d < 0 )) && (>&2 echo "up: Error: negative value provided") && return 1;
# remove last d directories from pwd, append "/" in case result is empty
cd "$(pwd | sed -E 's;(/[^/]*){0,'$d'}$;;')/";
}
Use it like this:
up 4
1
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– Donald Duck
Mar 13 '17 at 16:01
1
Agreed, I have added a few comments to help.
– mgild
Mar 13 '17 at 19:11
To be nitpicky, isn'tup 4
really equivalent tocd /some/dir/
assuming$PWD = "/some/dir/that/is/deeply/nested/"
?
– snapfractalpop
Aug 15 '18 at 23:28
add a comment |
You could write a function (it has to be a function, as you want to change the state of your shell itself, namely the working directory; an external command would affect only its own process).
Here's a function that will go up a number of levels passed as argument (default 1) in the physical directory structure (so, like cd -P ..
, n times):
up() {
# default parameter to 1 if non provided
declare -i d=${@:-1}
# ensure given parameter is non-negative. Print error and return if it is
(( $d < 0 )) && (>&2 echo "up: Error: negative value provided") && return 1;
# remove last d directories from pwd, append "/" in case result is empty
cd "$(pwd | sed -E 's;(/[^/]*){0,'$d'}$;;')/";
}
Use it like this:
up 4
You could write a function (it has to be a function, as you want to change the state of your shell itself, namely the working directory; an external command would affect only its own process).
Here's a function that will go up a number of levels passed as argument (default 1) in the physical directory structure (so, like cd -P ..
, n times):
up() {
# default parameter to 1 if non provided
declare -i d=${@:-1}
# ensure given parameter is non-negative. Print error and return if it is
(( $d < 0 )) && (>&2 echo "up: Error: negative value provided") && return 1;
# remove last d directories from pwd, append "/" in case result is empty
cd "$(pwd | sed -E 's;(/[^/]*){0,'$d'}$;;')/";
}
Use it like this:
up 4
edited Dec 20 '18 at 23:24
answered Mar 13 '17 at 15:41
mgild
313
313
1
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– Donald Duck
Mar 13 '17 at 16:01
1
Agreed, I have added a few comments to help.
– mgild
Mar 13 '17 at 19:11
To be nitpicky, isn'tup 4
really equivalent tocd /some/dir/
assuming$PWD = "/some/dir/that/is/deeply/nested/"
?
– snapfractalpop
Aug 15 '18 at 23:28
add a comment |
1
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– Donald Duck
Mar 13 '17 at 16:01
1
Agreed, I have added a few comments to help.
– mgild
Mar 13 '17 at 19:11
To be nitpicky, isn'tup 4
really equivalent tocd /some/dir/
assuming$PWD = "/some/dir/that/is/deeply/nested/"
?
– snapfractalpop
Aug 15 '18 at 23:28
1
1
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– Donald Duck
Mar 13 '17 at 16:01
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– Donald Duck
Mar 13 '17 at 16:01
1
1
Agreed, I have added a few comments to help.
– mgild
Mar 13 '17 at 19:11
Agreed, I have added a few comments to help.
– mgild
Mar 13 '17 at 19:11
To be nitpicky, isn't
up 4
really equivalent to cd /some/dir/
assuming $PWD = "/some/dir/that/is/deeply/nested/"
?– snapfractalpop
Aug 15 '18 at 23:28
To be nitpicky, isn't
up 4
really equivalent to cd /some/dir/
assuming $PWD = "/some/dir/that/is/deeply/nested/"
?– snapfractalpop
Aug 15 '18 at 23:28
add a comment |
Not exactly what you're asking for but you should look into pushd
and popd
. I find them much more useful for folder navigation than some cd...
alias
If you're going back and forth from a couple fixed areas, the usual thing is to have aliases.
alias proj1='cd /some/dir/containing/proj1'
alias proj2='cd /some/deeper/dir/structure/containing/proj2'
add a comment |
Not exactly what you're asking for but you should look into pushd
and popd
. I find them much more useful for folder navigation than some cd...
alias
If you're going back and forth from a couple fixed areas, the usual thing is to have aliases.
alias proj1='cd /some/dir/containing/proj1'
alias proj2='cd /some/deeper/dir/structure/containing/proj2'
add a comment |
Not exactly what you're asking for but you should look into pushd
and popd
. I find them much more useful for folder navigation than some cd...
alias
If you're going back and forth from a couple fixed areas, the usual thing is to have aliases.
alias proj1='cd /some/dir/containing/proj1'
alias proj2='cd /some/deeper/dir/structure/containing/proj2'
Not exactly what you're asking for but you should look into pushd
and popd
. I find them much more useful for folder navigation than some cd...
alias
If you're going back and forth from a couple fixed areas, the usual thing is to have aliases.
alias proj1='cd /some/dir/containing/proj1'
alias proj2='cd /some/deeper/dir/structure/containing/proj2'
answered Oct 25 '12 at 22:03
Rich Homolka
25.1k64366
25.1k64366
add a comment |
add a comment |
Instead of using aliases you could also use the following bash function:
function mcd() {
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
(or as a one-liner: function mcd() { up=""; for ((i=1; i<=$1;i++)); do up="${up}../"; done; cd $up; }
)
Adding this to your ~/.bashrc
file will make it available in your terminal and the building of a String ../../../../../../
before calling cd
will also make it possible to use cd -
to jump back to the start directory.
A more helpful implementation could also contain some user-input checks:
function mcd() {
if [[ $1 -lt 1 ]]; then
echo "Only positive integer values larger than 1 are allowed!" >&2
echo -e "ntUsage:nt======nntt# to go up 10 levels in your directorynttmcd 10nntt# to go up just 2 levelsnttmcd 2n" >&2
return 1;
fi
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
add a comment |
Instead of using aliases you could also use the following bash function:
function mcd() {
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
(or as a one-liner: function mcd() { up=""; for ((i=1; i<=$1;i++)); do up="${up}../"; done; cd $up; }
)
Adding this to your ~/.bashrc
file will make it available in your terminal and the building of a String ../../../../../../
before calling cd
will also make it possible to use cd -
to jump back to the start directory.
A more helpful implementation could also contain some user-input checks:
function mcd() {
if [[ $1 -lt 1 ]]; then
echo "Only positive integer values larger than 1 are allowed!" >&2
echo -e "ntUsage:nt======nntt# to go up 10 levels in your directorynttmcd 10nntt# to go up just 2 levelsnttmcd 2n" >&2
return 1;
fi
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
add a comment |
Instead of using aliases you could also use the following bash function:
function mcd() {
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
(or as a one-liner: function mcd() { up=""; for ((i=1; i<=$1;i++)); do up="${up}../"; done; cd $up; }
)
Adding this to your ~/.bashrc
file will make it available in your terminal and the building of a String ../../../../../../
before calling cd
will also make it possible to use cd -
to jump back to the start directory.
A more helpful implementation could also contain some user-input checks:
function mcd() {
if [[ $1 -lt 1 ]]; then
echo "Only positive integer values larger than 1 are allowed!" >&2
echo -e "ntUsage:nt======nntt# to go up 10 levels in your directorynttmcd 10nntt# to go up just 2 levelsnttmcd 2n" >&2
return 1;
fi
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
Instead of using aliases you could also use the following bash function:
function mcd() {
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
(or as a one-liner: function mcd() { up=""; for ((i=1; i<=$1;i++)); do up="${up}../"; done; cd $up; }
)
Adding this to your ~/.bashrc
file will make it available in your terminal and the building of a String ../../../../../../
before calling cd
will also make it possible to use cd -
to jump back to the start directory.
A more helpful implementation could also contain some user-input checks:
function mcd() {
if [[ $1 -lt 1 ]]; then
echo "Only positive integer values larger than 1 are allowed!" >&2
echo -e "ntUsage:nt======nntt# to go up 10 levels in your directorynttmcd 10nntt# to go up just 2 levelsnttmcd 2n" >&2
return 1;
fi
up=""
for ((i=1; i<=$1;i++)); do
up="${up}../"
done
cd $up
}
edited Aug 12 '14 at 9:59
joeeey
1,286620
1,286620
answered Aug 12 '14 at 8:10
WhoCares
112
112
add a comment |
add a comment |
Do you know about autojump? It's a third party hack, but can be useful in your scenario.
add a comment |
Do you know about autojump? It's a third party hack, but can be useful in your scenario.
add a comment |
Do you know about autojump? It's a third party hack, but can be useful in your scenario.
Do you know about autojump? It's a third party hack, but can be useful in your scenario.
answered Oct 25 '12 at 23:26
AnonymousLurker
5451616
5451616
add a comment |
add a comment |
Try the rarely used environment parameter CDPATH
. Then you might not have to explicitly set the level.
Example:
$ find workspace -type d
workspace
workspace/project1
workspace/project1/com
workspace/project1/com/java
workspace/project1/com/java/lang
$ CDPATH=".:~/workspace:~/workspace/project1:~/workspace/project1/com:~/workspace/project1/com/java:~/workspace/project1/com/java/lang"
$ cd com
$ pwd
~/workspace/project1/com
If working on multiple projects, you can make the CDPATH setting into a project specific environment file. And trigger it with a shim for additional automation.
I tend to use pushd and popd quite a lot. I tend to use CDPATH to let me hop between project trees rather than subdirs in a project - but at the moment I'm working on a lot of small projects, not a few big projects. :)
add a comment |
Try the rarely used environment parameter CDPATH
. Then you might not have to explicitly set the level.
Example:
$ find workspace -type d
workspace
workspace/project1
workspace/project1/com
workspace/project1/com/java
workspace/project1/com/java/lang
$ CDPATH=".:~/workspace:~/workspace/project1:~/workspace/project1/com:~/workspace/project1/com/java:~/workspace/project1/com/java/lang"
$ cd com
$ pwd
~/workspace/project1/com
If working on multiple projects, you can make the CDPATH setting into a project specific environment file. And trigger it with a shim for additional automation.
I tend to use pushd and popd quite a lot. I tend to use CDPATH to let me hop between project trees rather than subdirs in a project - but at the moment I'm working on a lot of small projects, not a few big projects. :)
add a comment |
Try the rarely used environment parameter CDPATH
. Then you might not have to explicitly set the level.
Example:
$ find workspace -type d
workspace
workspace/project1
workspace/project1/com
workspace/project1/com/java
workspace/project1/com/java/lang
$ CDPATH=".:~/workspace:~/workspace/project1:~/workspace/project1/com:~/workspace/project1/com/java:~/workspace/project1/com/java/lang"
$ cd com
$ pwd
~/workspace/project1/com
If working on multiple projects, you can make the CDPATH setting into a project specific environment file. And trigger it with a shim for additional automation.
I tend to use pushd and popd quite a lot. I tend to use CDPATH to let me hop between project trees rather than subdirs in a project - but at the moment I'm working on a lot of small projects, not a few big projects. :)
Try the rarely used environment parameter CDPATH
. Then you might not have to explicitly set the level.
Example:
$ find workspace -type d
workspace
workspace/project1
workspace/project1/com
workspace/project1/com/java
workspace/project1/com/java/lang
$ CDPATH=".:~/workspace:~/workspace/project1:~/workspace/project1/com:~/workspace/project1/com/java:~/workspace/project1/com/java/lang"
$ cd com
$ pwd
~/workspace/project1/com
If working on multiple projects, you can make the CDPATH setting into a project specific environment file. And trigger it with a shim for additional automation.
I tend to use pushd and popd quite a lot. I tend to use CDPATH to let me hop between project trees rather than subdirs in a project - but at the moment I'm working on a lot of small projects, not a few big projects. :)
answered Aug 12 '14 at 9:09
JezC
53025
53025
add a comment |
add a comment |
If you are previously on the target directory:
luna:/tmp % mkdir -p a/b/c/d
luna:/tmp % pwd
/tmp
luna:/tmp % cd a/b/c/d
luna:d % pwd
/tmp/a/b/c/d
luna:d % cd -
luna:/tmp % pwd
/tmp
luna:/tmp %
add a comment |
If you are previously on the target directory:
luna:/tmp % mkdir -p a/b/c/d
luna:/tmp % pwd
/tmp
luna:/tmp % cd a/b/c/d
luna:d % pwd
/tmp/a/b/c/d
luna:d % cd -
luna:/tmp % pwd
/tmp
luna:/tmp %
add a comment |
If you are previously on the target directory:
luna:/tmp % mkdir -p a/b/c/d
luna:/tmp % pwd
/tmp
luna:/tmp % cd a/b/c/d
luna:d % pwd
/tmp/a/b/c/d
luna:d % cd -
luna:/tmp % pwd
/tmp
luna:/tmp %
If you are previously on the target directory:
luna:/tmp % mkdir -p a/b/c/d
luna:/tmp % pwd
/tmp
luna:/tmp % cd a/b/c/d
luna:d % pwd
/tmp/a/b/c/d
luna:d % cd -
luna:/tmp % pwd
/tmp
luna:/tmp %
answered Nov 20 '15 at 14:51
masm
1216
1216
add a comment |
add a comment |
Take a look at DirB. It's a BASH script that allows you to create bookmarks as follows:
OPERATIONS
- s Save a directory bookmark
- g go to a bookmark or named directory
- p push a bookmark/directory onto the dir stack
- r remove saved bookmark
- d display bookmarked directory path
- sl print the list of directory bookmarks
Very simple, very effective.
add a comment |
Take a look at DirB. It's a BASH script that allows you to create bookmarks as follows:
OPERATIONS
- s Save a directory bookmark
- g go to a bookmark or named directory
- p push a bookmark/directory onto the dir stack
- r remove saved bookmark
- d display bookmarked directory path
- sl print the list of directory bookmarks
Very simple, very effective.
add a comment |
Take a look at DirB. It's a BASH script that allows you to create bookmarks as follows:
OPERATIONS
- s Save a directory bookmark
- g go to a bookmark or named directory
- p push a bookmark/directory onto the dir stack
- r remove saved bookmark
- d display bookmarked directory path
- sl print the list of directory bookmarks
Very simple, very effective.
Take a look at DirB. It's a BASH script that allows you to create bookmarks as follows:
OPERATIONS
- s Save a directory bookmark
- g go to a bookmark or named directory
- p push a bookmark/directory onto the dir stack
- r remove saved bookmark
- d display bookmarked directory path
- sl print the list of directory bookmarks
Very simple, very effective.
answered Feb 1 '16 at 19:42
rseal
11
11
add a comment |
add a comment |
Step further from @Grigory's answer:
function cd_up() {
if [ -z "$1" ]; then
cd ..
else
cd $(printf "%0.s../" $(seq 1 $1 ))
fi
}
alias 'cdd'='cd_up'
That is :
// go up 1 directory level
$cdd
// go up 3 directory level
$cdd 3
add a comment |
Step further from @Grigory's answer:
function cd_up() {
if [ -z "$1" ]; then
cd ..
else
cd $(printf "%0.s../" $(seq 1 $1 ))
fi
}
alias 'cdd'='cd_up'
That is :
// go up 1 directory level
$cdd
// go up 3 directory level
$cdd 3
add a comment |
Step further from @Grigory's answer:
function cd_up() {
if [ -z "$1" ]; then
cd ..
else
cd $(printf "%0.s../" $(seq 1 $1 ))
fi
}
alias 'cdd'='cd_up'
That is :
// go up 1 directory level
$cdd
// go up 3 directory level
$cdd 3
Step further from @Grigory's answer:
function cd_up() {
if [ -z "$1" ]; then
cd ..
else
cd $(printf "%0.s../" $(seq 1 $1 ))
fi
}
alias 'cdd'='cd_up'
That is :
// go up 1 directory level
$cdd
// go up 3 directory level
$cdd 3
answered Jan 12 '17 at 8:26
qun
29133
29133
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f449687%2fusing-cd-to-go-up-multiple-directory-levels%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Modifying bash to interpret
cd ...
where the number of '.' would be the number of levels to go up. Would this cause a conflict that I'm not aware of?– user48420
Apr 19 '16 at 17:48