Searching source directories
As part of my job, I work with a huge source tree structure. In last count, the tree has more than twenty five thousand directories and subdirectories. To get from one part to another in the tree, I use a set of aliases and bash functions. One of them is the following "goto" function.
The idea here is, there are a set of source directories that I normally go to, or work under directly. Then there are some directories that are more then one level deeper but are under these same source directories. Then there is the rest of the tree where I need to go from time to time.
So when I run "goto xyzd", I want it to run the search in three phases:
1. Check if xyzd is there as the first level subdirectory under any of the common directories I work on. This should be the most common scenario.
2. If not found, do a "find" based search on the specific commonly used source directories only.
3. If pass 2 also fails, do a complete search from root of the tree.
4. In any of the passes, if a directory is found, cd to it.
This works out well for me. There are enhancements that can be done (like, if more than one name is found, giving options), but so far I didn't find any need for it.
The script looks like the following. You will need to change the DEV_ROOT and SDIRS variables to match your tree.
The idea here is, there are a set of source directories that I normally go to, or work under directly. Then there are some directories that are more then one level deeper but are under these same source directories. Then there is the rest of the tree where I need to go from time to time.
So when I run "goto xyzd", I want it to run the search in three phases:
1. Check if xyzd is there as the first level subdirectory under any of the common directories I work on. This should be the most common scenario.
2. If not found, do a "find" based search on the specific commonly used source directories only.
3. If pass 2 also fails, do a complete search from root of the tree.
4. In any of the passes, if a directory is found, cd to it.
This works out well for me. There are enhancements that can be done (like, if more than one name is found, giving options), but so far I didn't find any need for it.
The script looks like the following. You will need to change the DEV_ROOT and SDIRS variables to match your tree.
function goto() {\
echo "Searching for $1 ...";
DEV_ROOT="root/dir/for/tree"
SDIRS="src/xxxx/usr.bin:src/xxxx/usr.sbin:src/xxxx/yyyy/common:src/xxxx/sbin";
# Pass 1
for i in `echo $SDIRS|sed 's/:/ /g'` ;
do
if [ -d $DEV_ROOT/$i/$1 ];
then
cd $DEV_ROOT/$i/$1;
return;
fi;
done;
echo "Couldn't find $1 in first pass... Trying second pass...";
# Pass 2
for i in `echo $SDIRS|sed 's/:/ /g'` ;
do
echo "Searching in $DEV_ROOT/$i..."
DIRPATH=`find $DEV_ROOT/$i -type d -name $1`
if [ "$DIRPATH" != '' ];
then
echo $DIRPATH
cd $DIRPATH
return;
fi
done;
echo "Couldn't find $1 in second pass... Trying full search ...";
# Pass 3
DIRPATH=`find $DEV_ROOT/src -type d -name $1`
if [ "$DIRPATH" != '' ];
then
cd $DIRPATH
return;
fi
echo "Failed to find directory $1."
}
0 TrackBacks
Listed below are links to blogs that reference this entry: Searching source directories.
TrackBack URL for this entry: http://samya.indiangeek.com/new/mtypes/MT/mt-tb.cgi/28
Leave a comment