Visit The New, Modern Unix Linux Community


Fast yet simple way to navigate directories

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Fast yet simple way to navigate directories
# 1  
Bug 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:
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:
Code:
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:
Code:
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.

Last edited by Scrutinizer; 04-27-2012 at 03:41 AM.. Reason: Code tags
# 2  
This User Gave Thanks to itkamaraj For This Post:
# 3  
sounds so much more convoluted than aliases or bind mounts.
This User Gave Thanks to mark54g For This Post:
# 4  
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?
# 5  
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,
Code:
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:
Code:
sc /etc n*scripts

or
Code:
sc /etc sys*/n*scripts

Indeed,
Code:
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.
# 6  
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..
Code:
$ export CDPATH=~:/etc
$ mkdir ~/kwyjibo
$ cd /
$ cd kwyjibo
/home/scrutinizer/kwyjibo
$pwd
/home/scrutinizer/kwyjibo
$ cd postfix
/etc/postfix


Last edited by Scrutinizer; 04-29-2012 at 02:00 AM..
# 7  
I did my homework.
Just like alias, CDPATH does have its limitation.

Suppose you are in user home directory:
Code:
/home/scrutinizer

and you want to get access to:
Code:
/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:
Code:
sc /home 1.1.1.1.1.1

and you can also go to any directories in between by typing:
Code:
sc /home 1.1.1.1


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #825
Difficulty: Medium
The term API seems to appear for the first time in the article of Ira W. Cotton, Data structures and techniques for remote computer graphics, published in 1968.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to Navigate to Windows Desktop?

I want to use my Desktop for saving files of my codes. Looking for Desktop in my OS (C : ) and couldn't find it anywhere. Is it possible to navigate to your desktop? If so how? By the way, I am using Cygwin. (6 Replies)
Discussion started by: DyslexicChciken
6 Replies

2. AIX

Do you need execute permission to navigate to a directory?

i have a user 'bart' which does not belong to apps group (as shown below) and i want him to be able to navigate to TEST directory.. i gave him read access but he cannot get through. when i added execute permission he was able to navigate to TEST drwxr-xr-- 3 draco apps 4096 Apr... (2 Replies)
Discussion started by: chipahoys
2 Replies

3. UNIX for Dummies Questions & Answers

help with simple unix file copy and output in directories

Hi, I am fairly new to unix, and am trying to copy all files with the name "*.cons" within a directory (and all of the many directories within it) to a new directory called "output". There are multiple nested directories, and I would like to just pull out the files with ".cons" and not the other... (5 Replies)
Discussion started by: euspilapteryx
5 Replies

4. Shell Programming and Scripting

script to navigate thrugh directories

Hi, I have a requiremnet where i need to apply logic on directories and sub-direcotories. Example: base="/a/b/c" base1="/a/b/c/d/e" subfolders in both base and base1 are same : es, ig, os var1=es var2=ig var3=os Now i have used the follwoing lines in my code and these are... (3 Replies)
Discussion started by: sussane
3 Replies

5. UNIX for Dummies Questions & Answers

How to navigate previous files one by one.

Hi can you tell me if i have opened no of files using vi editior then how can i navigate previous files one by one. Suppose i have opened five files using vi editor as below vi file1 file2 file3 file4 file5 and nom i am in the last file file5 then if i want to go to previous file file 4 and... (5 Replies)
Discussion started by: ajayshukla
5 Replies

6. UNIX for Dummies Questions & Answers

Viewing Directory Content as You Navigate Directories in UNIX

Hi, Can someone help me figure out how to view directory content while I navigate directories (without having to go to the actual directory and "ls-ing" it)? Is there some keyboard shortcut for this? For instance, it would be useful if I could see the content of a directory when I'm copying... (2 Replies)
Discussion started by: shelata
2 Replies

7. Shell Programming and Scripting

How Do I Navigate To A Directory???

I am trying to install some applications, on my linux OS and all the instructions are tell me that ill have to navigate to the directories and type the link that they provide, but i try typing 'dir' which shows me the directories but to access a particular one i can't remember how??? can anyone... (2 Replies)
Discussion started by: kprescod4158
2 Replies

8. UNIX for Dummies Questions & Answers

Can you navigate ABOVE the home directory?

Hello, How do I navigate to the level ABOVE the home directory? I have the following structure on my drive, bearing in my I'm using a cygwin port on Windows. C:\ C:\cygwin C:\cygwin\bin C:\cygwin\otherfolders C:\cygwin\home (the home directory) C:\cygwin\home\H (my user directory) ... (4 Replies)
Discussion started by: patwa
4 Replies

9. UNIX for Dummies Questions & Answers

changing directories (i'm sure there is a simple solution for this)

I just want to exit my script in a new directory from a bash shell. Problem is that the script internally changes to the directory I want to move to, however when exits is still in the original directory. Does that make sense? ie usage: goto null changing from /usr/bin/xtra/test/test3/ ... (8 Replies)
Discussion started by: Shakey21
8 Replies

Featured Tech Videos