Fast yet simple way to navigate directories

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.

use code tags

1 Like

sounds so much more convoluted than aliases or bind mounts.

1 Like

It seems to me that the use of quoting would be necessary. For example sc /etc *scripts will only give the desired result if no such pattern happens to exist in the current directory or you may get unexpected results, so sc /etc '*scripts' will be preferable.

Also, sc / somedir will probably take a long time, no?

Scrutinizer, many thanks for your editing, it looks much nicer and professional.
And thank itkamaraj for the link. I'm new to here and I need some time to get myself familiar with tags.

Scrutinizer, you are right,

sc /etc *scripts 

might get unexpected results if there are more than one folder name ending with string "scripts".
So usually I will provide more information to make sure I get the unique result:

sc /etc n*scripts

or

sc /etc sys*/n*scripts

Indeed,

sc / somedir 

takes a long time, so better avoid searching from root.

And for mark54g,
Alias is easy to use, but it does has its limitation:
You need to pre-define all your frequently used folders in .bash_profile.
But what if the folder you want to access is not defined in .bash_profile?
What are you gonna do?
For me, I will use FIND command to search the full path of folder, and filter out some unwanted results with GREP and CD it.
And that's exactly what has been done in this shell.

Hi you are welcome, and also to the Unix and Linux forums. Are you aware of CDPATH? That might also be of use, perhaps not exactly what you require, but it is fast..

$ export CDPATH=~:/etc
$ mkdir ~/kwyjibo
$ cd /
$ cd kwyjibo
/home/scrutinizer/kwyjibo
$pwd
/home/scrutinizer/kwyjibo
$ cd postfix
/etc/postfix

I did my homework.
Just like alias, CDPATH does have its limitation.

Suppose you are in user home directory:

/home/scrutinizer

and you want to get access to:

/home/scrutinizer/qwyzibo/1/1.1/1.1.1/1.1.1.1/1.1.1.1.1/1.1.1.1.1.1

In this case, CDPATH does not help very much.

But with sc, you can simply type:

sc /home 1.1.1.1.1.1

and you can also go to any directories in between by typing:

sc /home 1.1.1.1

Interesting. Both BASH and KSH have that...

1 Like

Good work!! :b:

How does it act if we get multiple results for one search?

Example:

/var/log/mylog1
/var/log/mylog2
/var/log/mylog2
sc /var/log *log*

From the first look of the script, I dont think it's meant to handle such situations!!

1 Like

It is POSIX even:

cd: ENVIRONMENT VARIABLES

1 Like

Thanks, admin_xor.:slight_smile:
If results for one search are more than one, this script will simply choose the first one. And in your case, it is

/var/log/mylog1

So if you want go to, say,

/var/log/mylog3

then you have to provide it with more specific information by typing

sc /var log/myl*3

or

sc /var log/mylog3