提高查找文件效率及减少grep命令输入的shell脚本函数
前言:对于需要切换目录、检索之类的操作,比如要cd到./xx/xx/xx/xx/xx/xx/inc目录,鼠标点选+拷贝,手指可真累啊。Linuxer说,要专业,要有效率,于是,有了这脚本。...
提示:将函数加到shell启动脚本中,或source即可。
function zxhelp() {
cat <<EOF
syntax
zxx [path] [,] {[extensions]} [grep-options] key-words
EOF
}
export ZZROOT="="
# Create temporary file in /tmp
if [ ! -f /tmp/zzroot ]; then
echo "./frameworks" >/tmp/zzroot
fi
# Display temporary file's text
function zxroot() {
echo `head -1 /tmp/zzroot | sed -e "s/\(^ *\)//" -e "s/\( *$\)//" | tr -d [:cntrl:]`
}
# Find matched directories
function zdd() {
local szroot=""
local sztarg=""
local szgrep=""
if [ $# -gt 1 ]; then
while [ $# -gt 0 ]; do
if [ $# -gt 1 -a -d $1 ]; then
szroot="$szroot $1 "; sztemp=$(echo -n " $szroot " | sed 's:\s./*\s::g' | sed 's/ \+//'); if [ -n "$sztemp" ]; then echo "$sztemp" >/tmp/zzroot; fi
elif [ "$1" = "$ZZROOT" ]; then
sztemp=`head -1 /tmp/zzroot | sed 's/ \+//' | sed 's/ \+$//' | tr -d [:cntrl:]`; szroot="$szroot $sztemp ";
else
sztmp=$(echo -n "$1" | sed -r 's/^[-]+//');
if [ "x$1" = "x$sztmp" ]; then
sztarg=$1;
else
szgrep="$szgrep $1";
fi
fi;
shift;
done
elif [ $# -gt 0 ]; then
sztarg=$1;
fi
if [ "x$sztarg" = "x" ]; then
echo "Func: Search target-path to find directories matched with grep-expression(keys, a regular string)"
echo -e "Usage:\033[1;32m zdd [path...] expression\033[0m"
else
if [ "x$szroot" = "x" ]; then
szroot=.
fi
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 -iname "*$sztmp*" | sed -r 's/\s+/@ZBLKZ@/g' | grep -i "$sztarg");
total=0;
sztmp="";
szgrep=$(echo "--color $szgrep" | sed -r "s/^[ ]+//" | tr -d [:cntrl:]);
for s in $slist; do
sztmp=$(echo $s | sed -r 's/@ZBLKZ@/ /g' | tr -d [:cntrl:]); total=$(expr $total "+" 1); echo "$sztmp" | grep $szgrep "$sztarg";
done
if [ $total -gt 1 ]; then
itemp=$total; # for s in $slist; do echo $s | grep --color "$sztarg"; done
elif [ $total -gt 0 ]; then
if [ -d "$sztmp" ]; then cd "$sztmp"; fi
else
echo -e "\033[1;33m--------------------------------------------------------------------------------\033[0m"
echo -e "\E[31m Not found directory: $sztarg \E[0m";
echo -e "\033[1;33m--------------------------------------------------------------------------------\033[0m"
fi
fi
}
# Find matched files
function zff() {
local szroot="" ## path to find
local sztarg="" ## key of filename
local szfilt="" ## extensions ?
local szgrep="" ## grep options ?
local sicase="" ## grep -i ?
local silent="" ## no prompt message
while [ $# -gt 0 ]; do
if [ "x$1" = "x-i" ]; then sicase="-i"; ## grep -i
elif [ "x$1" = "x-s" ]; then silent="-s"; ## silent
elif [ $# -gt 1 -a "$1" = "$ZZROOT" ]; then ## use cached directories
sztemp=`head -1 /tmp/zzroot | sed 's/ \+//' | sed 's/ \+$//' | tr -d [:cntrl:]`; szroot="$szroot $sztemp ";
elif [ $# -gt 1 -a -d "$1" ]; then ## assigned directory
szroot="$szroot $1 "; sztemp=$(echo -n " $szroot " | sed 's:\s./*\s::g' | sed 's/ \+//'); if [ -n "$sztemp" ]; then echo "$sztemp" >/tmp/zzroot; fi
else
sztmp=$(echo -n "$1" | sed -r 's/^[-]\+//');
if [ "$sztmp" != "$1" ]; then ## grep options
szgrep="$szgrep $1";
else
sztemp=$(echo -n "$1" | sed 's/\.//g'); ## likes ".*xxx.*.\(c|cpp\|h|java\)", *.java, ?
sztarg="$1"
if [ "$sztemp" != "$sztarg" ]; then
szfilt=$(echo -n "${sztarg##*.}" | sed 's:\\\+|\+:|:g' | sed 's:|:\\|:g' | sed 's:\\*[()]\+::g');
sztarg=$(echo -n "${sztarg%.*}" | sed 's/^[*\.]\+//' | sed 's:[*\.]\+$::' );
fi
sztarg=$(echo "$sztarg" | sed 's/*/\.*/g'); ## repl '*' to '.*'
if [ "$szfilt" != "" -a "$sztarg" = "" ]; then
sztarg='.*';
fi
fi
fi;
shift;
done
if [ "$sztarg" = "" -a "$szfilt" = "" ]; then
echo "Func: Search target-path to find files matched with grep-expression(keys, a regular string)"
echo -e "Usage:\033[1;32m zff [path...] [extensions...] [-s] [-i] [grep-options...] expression\033[0m"
else
if [ "x$szroot" = "x" ]; then
szroot=.
fi
if [ "$szfilt" = "" ]; then
#
# Case 1: find files without extensions-filter
#
sztemp=$(echo "$sztarg" | grep "\.\*");
if [ $? -eq 0 ]; then
slist=$(find $szroot -type f -iregex "$sztarg" | sed -r 's/\s+/@ZBLKZ@/g');
else
slist=$(find $szroot -type f -iregex ".*$sztarg.*" | sed -r 's/\s+/@ZBLKZ@/g' | grep $sicase "$sztarg");
fi
elif [ "x$sztarg" = "x.*" ]; then
#
# Case 2: find all files with only extentions-filter
#
slist=$(find $szroot -type f -iregex ".*\.\($szfilt\)" | sed -r 's/ /@ZBLKZ@/g');
else
#
# Case 3: find files with extentions-filter & target-filter
#
sztemp=$(echo "$sztarg" | grep "\.\*");
if [ $? -eq 0 ]; then
slist=$(find $szroot -type f -iregex "$sztarg\.\($szfilt\)" | sed -r 's/ /@ZBLKZ@/g');
else
slist=$(find $szroot -type f -iregex ".*$sztarg.*\.\($szfilt\)" | sed -r 's/ /@ZBLKZ@/g' | grep $sicase "$sztarg");
fi
fi
total=0;
sztmp="";
if [ "x$sztarg" = "x.*" ]; then # This is case 2, ...
for s in $slist; do
sztmp=$(echo $s | sed -r 's/@ZBLKZ@/ /g' | tr -d [:cntrl:]); total=$(expr $total "+" 1); echo "$sztmp" | grep $sicase --color "\.\($szfilt\)";
done
else
szgrep=$(echo "--color $szgrep" | sed -r "s/^[ ]+//" | tr -d [:cntrl:]);
for s in $slist; do
sztmp=$(echo $s | sed -r 's/@ZBLKZ@/ /g' | tr -d [:cntrl:]); total=$(expr $total "+" 1); echo "$sztmp" | grep $sicase $szgrep "$sztarg";
done
fi
if [ "$silent" = "" ]; then
echo -e "\033[1;33m--------------------------------------------------------------------------------\033[0m"
if [ $total -gt 1 ]; then
echo -e "\033[1;33mTotal found $total files ...\033[0m"
elif [ $total -gt 0 ]; then
if [ -f "$sztmp" ]; then
echo -n "$sztmp" | xsel -b; #xsel:select text into clipboard
else
echo -e "\E[31m \"$sztmp\" isn't a valid file \E[0m";
fi
else
echo -e "\E[31m Not found file: $sztarg \E[0m";
fi
echo -e "\033[1;33m--------------------------------------------------------------------------------\033[0m"
fi
fi
}
# Grep matched key-string in java,c,c++,h files
function zjc() {
if [ $# -lt 1 ]; then
echo -e "\033[1;32m find . -type f -iregex '.*\.\(c\|h\|cpp\|cc\|S\|java\|aidl\)' -print0 | xargs -0 grep --color -H -n $@\033[0m"
else
szroot="";
while [ $# -gt 0 ] && [ -d "$1" -o "$1" = "$ZZROOT" ]; do
if [ "$1" = "$ZZROOT" ]; then
sztemp=`head -1 /tmp/zzroot | sed 's/ \+//' | sed 's/ \+$//' | tr -d [:cntrl:]`; szroot="$szroot $sztemp ";
else
szroot="$szroot $1 "; sztemp=$(echo -n " $szroot " | sed 's:\s./*\s::g' | sed 's/ \+//'); if [ -n "$sztemp" ]; then echo "$sztemp" >/tmp/zzroot; fi
fi
shift;
done
if [ "x$szroot" = "x" ]; then
szroot='.'
fi
find $szroot -type f -iregex '.*\.\(c\|h\|cpp\|cc\|S\|java\|aidl\)' -print0 | xargs -0 grep --color -H -n $@
fi
}
# Grep matched key-string in c,c++,h files
function zcx() {
if [ $# -lt 1 ]; then
echo -e "\033[1;32m find . -type f -iregex '.*\.\(c\|h\|cpp\|cc\|S\|cxx\|hpp\|hxx\)' -print0 | xargs -0 grep --color -H -n $@\033[0m"
else
szroot="";
while [ $# -gt 0 ] && [ -d "$1" -o "$1" = "$ZZROOT" ]; do
if [ "$1" = "$ZZROOT" ]; then
sztemp=`head -1 /tmp/zzroot | sed 's/ \+//' | sed 's/ \+$//' | tr -d [:cntrl:]`; szroot="$szroot $sztemp ";
else
szroot="$szroot $1 "; sztemp=$(echo -n " $szroot " | sed 's:\s./*\s::g' | sed 's/ \+//'); if [ -n "$sztemp" ]; then echo "$sztemp" >/tmp/zzroot; fi
fi
shift;
done
if [ "x$szroot" = "x" ]; then
szroot='.'
fi
find $szroot -type f -iregex '.*\.\(c\|h\|cpp\|cc\|S\|cxx\|hpp\|hxx\)' -print0 | xargs -0 grep --color -H -n $@
fi
}
# Grep matched key-string in java files
function zjx() {
if [ $# -lt 1 ]; then
echo -e "\033[1;32m find . -type f -iregex '.*\.\(java\|aidl\)' -print0 | xargs -0 grep --color -H -n $@\033[0m"
else
szroot="";
while [ $# -gt 0 ] && [ -d "$1" -o "$1" = "$ZZROOT" ]; do
if [ "$1" = "$ZZROOT" ]; then
sztemp=`head -1 /tmp/zzroot | sed 's/ \+//' | sed 's/ \+$//' | tr -d [:cntrl:]`; szroot="$szroot $sztemp ";
else
szroot="$szroot $1 "; sztemp=$(echo -n " $szroot " | sed 's:\s./*\s::g' | sed 's/ \+//'); if [ -n "$sztemp" ]; then echo "$sztemp" >/tmp/zzroot; fi
fi
shift;
done
if [ "x$szroot" = "x" ]; then
szroot='.'
fi
find $szroot -type f -iregex '.*\.\(java\|aidl\)' -print0 | xargs -0 grep --color -H -n $@
fi
}
# Grep matched key-string in .sh,.mk files
function zmk() {
if [ $# -lt 1 ]; then
echo -e "\033[1;32m find . -type f \( -name '*\.mk' -o -name '*\.sh' -o -name 'Makefile' \) -print0 | xargs -0 grep --color -H -n $@\033[0m"
else
szroot="";
while [ $# -gt 0 ] && [ -d "$1" -o "$1" = "$ZZROOT" ]; do
if [ "$1" = "$ZZROOT" ]; then
sztemp=`head -1 /tmp/zzroot | sed 's/ \+//' | sed 's/ \+$//' | tr -d [:cntrl:]`; szroot="$szroot $sztemp ";
else
szroot="$szroot $1 "; sztemp=$(echo -n " $szroot " | sed 's:\s./*\s::g' | sed 's/ \+//'); if [ -n "$sztemp" ]; then echo "$sztemp" >/tmp/zzroot; fi
fi
shift;
done
if [ "x$szroot" = "x" ]; then
szroot='.'
fi
find $szroot -type f \( -name '*\.mk' -o -name '*\.sh' -o -name 'Makefile' \) -print0 | xargs -0 grep --color -H -n $@
fi
}
# Grep matched key-string in matched files
function zxx() {
if [ $# -lt 1 ]; then
echo -e "\033[1;32m find . -type f -iregex '.*\.\(\)' -print0 | xargs -0 grep --color -H -n $@\033[0m"
else
szroot=""; szexts=""; #while [ $# -gt 0 -a -d "$1" ]; do szroot="$szroot $1"; shift; done
while [ $# -gt 0 ]; do
if [ -d "$1" ]; then
szroot="$szroot $1 "; sztemp=$(echo -n " $szroot " | sed 's:\s./*\s::g' | sed 's/ \+//'); if [ -n "$sztemp" ]; then echo "$sztemp" >/tmp/zzroot; fi
elif [ "$1" = "$ZZROOT" ]; then
sztemp=`head -1 /tmp/zzroot | sed 's/ \+//' | sed 's/ \+$//' | tr -d [:cntrl:]`; szroot="$szroot $sztemp ";
else
sztemp=$(echo -n "$1" | sed 's/^\**\.//' | sed 's/^[\\]*(//'); ## start with "*." or "(" ?
if [ "$sztemp" = "$1" ]; then
break;
else
szexts=$(echo -n "$sztemp" | sed 's:\\\+|\+:|:g' | sed 's:|:\\|:g' | sed 's:\\*[()]\+::g');
fi
fi;
shift;
done
if [ "x$szroot" = "x" ]; then
szroot=.
fi
## echo "find $szroot -type f -iregex '.*\.\($szexts\)' -print0 | xargs -0 grep --color -H -n $@"; ## | tr -d [:cntrl:] | xsel -b
find $szroot -type f -iregex ".*\.\($szexts\)" -print0 | xargs -0 grep --color -H -n $@
fi
}
# Template to find java & c++ function/interface
alias zxt_jc_func="echo -e \"\033[1;33mTemplate for grep functions/interfaces.\n grep --color -n '\w\+(\(\w*[\s\*&]*\s*\w*\[*\]*\,*\)*)\s*{*$' \033[0m\"; echo \"grep --color -n '\w\+(\(\w*[\s\*&]*\s*\w*\[*\]*\,*\)*)\s*{*$'\" | tr -d [:cntrl:] | xsel -b; "
export zxt_cx_comment=":[0-9]\+:\s*/*\*"
export zxt_fn_javacxx="grep \"^\s\+\(\w\+\s*[\*&]*\(\[\]\)*\(<\w\+>\)*\s\+\)\+\w\+\s*(\""
function zxif() { ## grep interfaces/functions
if [ $# -gt 0 ]; then
grep "^\s*\(\w\+\s*[\*&]*\(\[\d*\]\)*\(<\w\+>\)*\s\+\)\+\w\+\s*(" $@
else
echo -e "\033[1;32m zxi filename \033[0m"
fi
}