Is there a command for going back a number of steps in a directory, without using cd?
I'm constantly going 'cd ../../../../'. Is there a command/alias that could let me go 'cmd 4' and I'd be taken back 4 directories?
Put this in your ~/.bashrc
:
cdup() {
levels=${1-1}
while ((levels--)); do
cd ..
done
}
(The name cdup
comes from the corresponding FTP command, just FYI.)
I was taught to use 'pushd' and 'popd' for such circumstances.
For example, type 'pushd .' and then 'cd /home'. Now type 'popd' and you will be back to where you started.
'pushd'/'popd' is a stack, you can push as many directories on there as you like, but it is last on, first off when you popd.
Sure, why not:
up() { [ $# = 0 ] && cd .. && return [ $1 = 0 ] && return cd .. && up $(($1 - 1)) }
Quick and dirty:
cmd () { dir=.; for i in $(seq 1 $1); do dir=$dir/..; done; pushd $dir; }
Formulated to only change directory once.
Here is an alternative way:
function cdup
{
cd $(for ((i=0 ; i<$1 ;i++)); do printf "../" ; done)
}