Create a new shell file and copy the following.
b.sh
=======
#!/bin/sh
_backfunc(){
if [ "$1" -eq 1 ]; then
cd ..
elif [ "$1" -eq 2 ]; then
cd ../..
elif [ "$1" -eq 3 ]; then
cd ../../..
elif [ "$1" -eq 4 ]; then
cd ../../../..
fi
}
alias back='_backfunc'
_backfunc(){
if [ "$1" -eq 1 ]; then
cd ..
elif [ "$1" -eq 2 ]; then
cd ../..
elif [ "$1" -eq 3 ]; then
cd ../../..
elif [ "$1" -eq 4 ]; then
cd ../../../..
fi
}
alias back='_backfunc'
Now run this script in the current terminal as source b.sh
From now on you can go back upto 4 directory by using following statements:
back 1 # to go back 1 directory
back 2 # to go back 2 directory
back 3 # to go back 3 directory
back 4 # to go back 4 directory
You can add more statements like to go for a particular directory with a number or string by adding more elif statements on the above code. You can also change the "back" to whatever command you want by changing the "back" in the code.
We can also implement multiple back from current directory by using while loop like shown below :
bloop.sh
==============
#!/bin/sh
_backfunc(){
count=$1
while [ "$count" -ne 0 ]
do
cd ..
count=$(( count - 1 ))
done
}
alias back='_backfunc'
_backfunc(){
count=$1
while [ "$count" -ne 0 ]
do
cd ..
count=$(( count - 1 ))
done
}
alias back='_backfunc'
To permanently make this alias you can add "source /home/USER/dir/bloop.sh" in the ~/.bashrc and from then on every new terminal will automatically detect the back statement. You can run the following script to add the file to the ~/.bashrc.
installbloop.sh
===============
#!/bin/sh
#Put the bloop.sh and this file in the same directory to install at start up.
pw=$(pwd)
pw="source $pw/bloop.sh"
b="$HOME/.bashrc"
echo "$pw">>$b
echo "Added $pw to $b file\n"
#Put the bloop.sh and this file in the same directory to install at start up.
pw=$(pwd)
pw="source $pw/bloop.sh"
b="$HOME/.bashrc"
echo "$pw">>$b
echo "Added $pw to $b file\n"
This little bloop code can save your time a lot as it is for me!
No comments:
Post a Comment