When I want to go one directory up I use
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 ..
?
When I want to go one directory up I use
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 ..
?
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.
So if you're in directory 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.
Make alias (in you ~/.bashrc)
function cd_up() {
cd $(printf "%0.0s../" $(seq 1 $1));
}
alias 'cd..'='cd_up'
and use:
$ cd.. 7
UPD: Or make more powerfull variant, cd to dir name in current path:
# cd up to n dirs
# using: cd.. 10 cd.. dir
function cd_up() {
case $1 in
*[!0-9]*) # if no a number
cd $( pwd | sed -r "s|(.*/$1[^/]*/).*|\1|" ) # search dir_name in current path, if found - cd to it
;; # if not found - not cd
*)
cd $(printf "%0.0s../" $(seq 1 $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
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.
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:
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:
_..()
{
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
Do not remember from where I copied this function, but it works great for me (you can put in .bashrc
file for example)
up() { local p= i=${1:-1}; while (( i-- )); do p+=../; done; cd "$p$2" && pwd; }
e.g. to go 7 directories up it is necessary to type up 7
You can do it like this cd ../../../../../../../
There is a cool article about some hacks you can apply to improve you navigation.
You could add the following function to your .bashrc
:
# ~/.bashrc
up() {
for D in $(seq 1 $1);
do
cd ..
done
}
So the usage would simply be:
user ~ $ up 3
user / $
This nifty function supports going up in both directions.
If you are in /a/b/c/d then 'up 1' will take you to /a/b/c, and so on. Thats pretty standard and covered in most of the other answers.
Now, for the special part; using negative numbers takes you up from the other direction. So, if you are in /a/b/c/d, 'up -1' will take you to /a, and so on.
function up() {
UP=$1
if [[ $UP =~ ^[\-0-9]+$ ]]; then
if ((UP<0)); then
UP=${UP#-}
cd $(echo $PWD | cut -d/ -f1-$((UP+1)))
else
cd $(printf "%0.s../" $(seq 1 ${UP}));
fi
fi
}
Available for download/install on github.
for fish users,
function cdd
for x in (seq $argv)
cd ..
end
end
and save it to your: ~/.config/fish/functions/cdd.fish
and just $ cdd 3
To go up 3 directories.
Hm, I don't think so.
But you can write cd ../../../..
(and so on) instead of 7 times cd ..
I've made a small utility that makes navigating deep directory structures a bit easier: https://github.com/ianatha/up.
With up
installed, you could:
$ pwd
/Users/jdoe/Developer/backend/src/main/java/com/example/service/db/
$ up example
$ pwd
/Users/jdoe/Developer/backend/src/main/java/com/example/
$ up 4
$ pwd
/Users/jdoe/Developer/backend/src/
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 $1
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`