If you are like me, typing CD command again and again would quickly get tiresome and wonder there must be a better way to do it.
I have done some searching on Google and this forum. The results I get (using alias, CDPATH or PUSHD) do not satisfy me completely, so I decide to do it my way.
Fortunately, I do not need to reinvent the wheel. Linux already provides us with great tools, and all I need to do are just to glue them together.
Here is how:
Step1: Look for the directory using FIND command.
Step2: Filter out the results with GREP.
Final step: CD the directory.
###################################################
Bash code:
#!/bin/bash
# File name: �sc� stands for simple cd.
# Functionalities: Simple way to navigate directories.
# Created by: IKE0000
# Created date: 2012/4/27
# If only one parameter is provided, then sc works just like CD command
if (($#<2))
then cd $1;
else
# Define frequently used directories here.
# I borrow the concept from CDPATH.
# For example:
# �o� for ORACLE
# �a� for Apache
if [[ $1 == "o" ]]
then
search="/u01";
elif [[ $1 == "a" ]]
then
search="/etc/httpd";
else
search="$1";
fi;
IFS="/";
array=($2);
name1=${array[0]};
name2=${array[1]};
unset IFS
# Build up the search command
if (( ${#array[@]} < 2 ))
then
cmd="find $search -name $name1 -type d"
else
# We need to filter out the result in backward order
cmd="find $search -name $name2 -type d | grep $name1"
fi;
cd $(eval $cmd)
fi;
echo -e "\e[1;34m" $(pwd) "\E[0m"
###################################################
Configuration:
Step1: Change file permission to make it available to other users:
chmod u+x sc.sh
Step2: Copy sc.sh to /bin/sc. Or if you don't have the permission to do that, you can add the folder in which sc.sh is located to PATH variable in .bash_profile.
Step3: Add an alias in .bash_profile, this little dot is required change your current directory:
alias sc=�. sc�
Usage:
sc /etc works just like CD command
sc /etc *scripts will take you to :
/etc/sysconfig/network-scripts
sc o udum* will take you to:
/u01/app/oracle/admin/orcl/udump
sc o euser/admin will take you to:
/u01/app/oracle/oracle/product/10.2.0/db_2/oc4j/j2ee/oc4j_applications/applications/em/em/euser/admin
Hope it will be useful to you.