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.
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
}
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.
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
You could write a function like this:
Put that in your
~/.bashrc
, then you can run e.g.up 7
to go up 7 directories. You could override cd to allowcd 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, usepushd
andpopd
-> google for more information.Or, if you know you have to
cd
to 7th grand parent very often, you could create an alias, like: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.
source
Be concise.
You can create aliases to do the work:
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.Coming from the Windows world, Alt + Up Arrow navigates to the parent directory in Windows Explorer. So I made something like this in
~/.inputrc
: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.
or
result:
Of course I also add aliases in my
.bashrc
file using the above functionUsing aliases with dots as names: