I often find myself doing commands like this
cd ../../../../../
Is there a shortcut I can use that will cd ../ n times?
I often find myself doing commands like this
cd ../../../../../
Is there a shortcut I can use that will cd ../ n times?
No, there is no existing command for this, but it is trivial to write one. Add these lines to your ~/.bashrc
file (thanks to D. Ben Knoble for pointing out that the variable should be local
):
function ncd(){
local path=""
for ((i=1;i<=$1;i++)); do
path="../$path"
done
echo "Moving to $path" >&2
cd "$path"
}
Save the file, open a new terminal, and you can now move N directories up using ncd N
. For example:
$ pwd
/home/terdon/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9
$ ncd 5
Moving to ../../../../../
$ pwd
/home/terdon/dir1/dir2/dir3/dir4
$ ncd 2
Moving to ../../
$ pwd
/home/terdon/dir1/dir2
Coming from the Windows world, Alt + Up Arrow navigates to the parent directory
in Windows Explorer. So I made something like this in ~/.inputrc
:
"\33\33[A": "cd ..\n"
then pressing Alt + Up Arrow moves to the parent directory in the terminal. You have to press multiple times of course to move higher, but I have found it to be very fast. Also you can change the shortcut to your liking.
"\eu": "cd ..\n"
.inputrc can be immediately reloaded with bind -f ~/.inputrc
Commented
Apr 7, 2022 at 18:51
You can easily create aliases or shell functions to perform the given task. I personally use these:
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
and so on, manually defined for up to many-many dots in my .bashrc
.
A shell function could instead easily take a numeric parameter, and execute cd ..
in a loop that many times.
Similar to @terdon's answer, with slightly more natural usage and output:
# go up
up() {
local count="${1:-1}"
local path=../
while (( --count > 0 )) ; do
path="$path"../
done
echo "cd -- $path"
cd -- "$path"
}
This defines a function up
, which has the following properties:
up n
will go up n
directoriesup
will go up exactly one directory (i.e., the default for n
is 1)shopt -s autocd
(hence the --
, which are
technically unnecessary)OLDPWD
is set correctly to the current value of PWD
, rather
than one of the intermediate directories. This means that up 5; cd -
works
as expected.local
makes a lot of sense, I should have thought of that myself! Then, you are defaulting to cd ../
when no input is passed, which is also good, but you don't actually explain any of these benefits, so they aren't obvious to the novice. The --
is pointless since you know the argument will always be a variation of ../
. And you could simplify to while (( --count > 0 )) ; do
to avoid the needless repetition of (( count-- ))
. But this is indeed a better version than mine, it's a shame you don't point out the benefits.
local
. My point in saying it's more confusing is - maybe subjective: for
is more readable than while
with a predecrement. Also, what does ${1:-1}
do? If i did not oversee something, that's the same as just $1
. I would also quote everything, like path="${path}../"
.
Commented
Jan 13, 2020 at 8:10
Using the printf hack:
function updir(){
cd $(printf '../%.0s' {1..$1})
}
E.G. updir 5
will move you up 5 directories.
0
also can be dropped, so printf '../%.s'
works just as well
Commented
Jan 13, 2020 at 8:07
Another possibility to consider is physically sending the keys if you have xdotool
:
xdotool type ../
Assign it to a keyboard hotkey through Keyboard -> Shortcuts dialog. Then you can press the hotkey a required number of times.
If you use zsh you can use repeat:
repeat 5 { cd ..}
repeat 5 cd ..
works. (I can’t figure out how to get it to work with braces.)
Commented
Jan 12, 2020 at 4:09
In Zsh, you can simply add a single dot per level, i.e.
cd ...
equals to cd ../..
andcd ......
equals to cd ../../../../..
Unfortunately, this cannot be used together with other path components, i.e. commands like
cd ...../foo
don’t work.
See also more tips on the cd
command in Zsh.
I have used this for years:
alias up=" cd .."
alias up2="cd ../.."
alias up3="cd ../../.."
alias up4="cd ../../../.."
alias up5="cd ../../../../.."
alias up6="cd ../../../../../.."
alias up7="cd ../../../../../../.."
alias up8="cd ../../../../../../../.."
alias up9="cd ../../../../../../../../.."