if I'm in a very deep directory a/b/c/d/e/f/g/h/i/j
and want to come back a/b/c
, I have to use ../../../../../../../
.
Is there command I can pass through a number, e.g. cd up 7
, to speed this operation up?
You could write a function like this:
up() {
local path i
for (( i=0; i < $1; i++ )); do
path+=../
done
cd "$path"
}
Put that in your ~/.bashrc
, then you can run e.g. up 7
to go up 7 directories. You could override cd to allow cd up 7
too, but just making a new command is shorter and less hassle.
If you are toggling between 2 directories, you can use cd -
to switch between both.
If you want to bookmark a few directories that you would probably cd
to often, use pushd
and popd
-> google for more information.
Or, if you know you have to cd
to 7th grand parent very often, you could create an alias, like:
alias cd7up='cd ../../../../../../../'
pushd
and popd
which are really useful
Mar 7, 2012 at 19:40
Creating an alias would work as a temporary solution, however if you want something more permanent that doesn't confine you to your presets I suggest writing a function to do this and including it in your .bashrc file.
# Go up directory tree X number of directories
function up() {
COUNTER="$@";
# default $COUNTER to 1 if it isn't already set
if [[ -z $COUNTER ]]; then
COUNTER=1
fi
# make sure $COUNTER is a number
if [ $COUNTER -eq $COUNTER 2> /dev/null ]; then
nwd=`pwd` # Set new working directory (nwd) to current directory
# Loop $nwd up directory tree one at a time
until [[ $COUNTER -lt 1 ]]; do
nwd=`dirname $nwd`
let COUNTER-=1
done
cd $nwd # change directories to the new working directory
else
# print usage and return error
echo "usage: up [NUMBER]"
return 1
fi
}
Be concise.
alias c1='cd ../'
alias c2='c1; c1'
alias c3='c2; c1'
alias c4='c3; c1'
alias c5='c4; c1'
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.
Alt
+Down Arrow
after to move back to previous child directory.
Jan 12, 2020 at 3:06
pushd
and down arrow call popd
: opensource.com/article/19/8/navigating-bash-shell-pushd-popd
Jan 12, 2020 at 15:51
You can create aliases to do the work:
alias cd..2="cd ../.."
alias cd..3="cd ../../.."
alias cd..4="cd ../../../.."
alias cd..5="cd ../../../../.."
And then you can just type cd..5
to go up 5 levels.
To make those aliases available in future logins, you can add the above to the .bash_profile
file in your home directory.
function cdl() {
local arguments;
local level_string;
local counter=1;
# first argument is how many levels you wish to traverse
local level=$1;
# grab any argument after the initial levels you wish to traverse
for var in "$@"; do
if [ $counter -gt 1 ]; then
arguments="$arguments/$var";
fi
counter+=1;
done
# build string based on how many levels you want to go up
if [ $level -gt 1 ]; then
counter=1;
while [ $counter -le $level ]; do
level_string="../$level_string";
let counter+=1;
done
fi
# execute command
cd $level_string$arguments
}
# Example:
#-----------------
# /usr/local/src/test/directory/blah> cdl 3 i want to be here
or
# /usr/local/src/test/directory/blah> cdl 3 i/want/to/be/here
result:
# /usr/local/src/i/want/to/be/here>
#-----------------
Of course I also add aliases in my .bashrc
file using the above function
alias cd2='cdl 2'
alias cd3='cdl 3'
alias cd4='cdl 4'
alias cd5='cdl 5'
Using aliases with dots as names:
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias ......='cd ../../../../../'