Linux bash function, jump to subdirectory

Foreword:

In Linux, a colleague who maintains the kernel code thinks that layer by layer CD is too troublesome. For example, if you want to go to an include directory, you need to cd ./xx/xx/xx/xx/xx/xx/incldue, so he helped write a script Functions can jump directly to subdirectories.

function

function cdx() {

	local szroot=.
	local sztarg=""
		if [ $# -gt 1 ]; then
		if [ -d $1 ]; then szroot=$1; sztarg=$2; else szroot=$2; sztarg=$1; fi
		elif [ $# -gt 0 ]; then
	 	sztarg=$1;
	fi
	if [ "x$sztarg" = "x" ]; then
		echo "Usage: zz root-directory target-directory"	
	else
		sztmp=$(echo $sztarg | awk '{s=$0; do{match(s,/\//); if(RLENGTH>0){s=substr(s,RSTART+1);}}while(RLENGTH>0); printf(s); }')
			slist=$(find $szroot -type d -name "*$sztmp" | grep "$sztarg$");
		total=0;
		sztmp="";
			for s in $slist;
			do
			sztmp=$s; total=$(expr $total "+" 1);
			done
		if [ $total -gt 1 ]; then
			for s in $slist; do echo $s | grep --color "$sztarg"; done
		else
			if [ -d $s ]; then cd $sztmp; fi
		fi
	fi
}

usage:

vi ~/.bash_profile  # 编辑配置文件
# 将上述函数贴入.bash_profile
# 保存,然后,刷新
source ~/.bash_profile

carried out

cdx [root-directory] target-directory

example:

cdx ~/3.9/kernel x86

Guess you like

Origin blog.csdn.net/hylaking/article/details/86062164