Basic Search utility

The following is a searching utility for file based searching in linux environment. The code works perfectly for Ubuntu-12.04 version, but as it is untested on other versions (some basic tweeks might be required on other versions of linux).

To start using this utility, all you need to do is copy the same code in a bash file (.sh file), and after setting the execute permissions, source the same in .profile or .bashrc file on home directory.

The file can be named 'searchUtil.sh' and for sourcing, use the following command:


source ${filePath}/searchUtil.sh

//Code follows

#!/usr/bin/env bash

#======================================================================
# File: searchUtil.sh
# Author: Lovish
#----------------------------------------------------------------------
# Purpose: Searching utility using 'find' and 'grep' for ease of usage.
#======================================================================
__seHelp () {
cat << EOF
------------------------------------------------------------------------
# 'se' utility to simplify file search (extension based)
# Usage 1 : se -- (Searching Extension) [arguments]
# Usage 2 : se (grep keyword) (Searching Extension) [arguments]
#
# NOTE:
# 1. {Usage 2 automatically supports ignorecase}
# 2. {Arguments are defined for find command args}
------------------------------------------------------------------------
EOF
}
unset se
se () {
[ $# -lt 2 ] \
&& __seHelp \
&& return 1
local FIND=`which find`
[ $? -ne 0 ] && echo "'find' command not found" && exit 1
local GREP=`which grep`
[ $? -ne 0 ] && echo "'grep' command not found" && exit 1
local __searchWith="$2"
local __flag="$1"
shift 2
case ${__flag} in
"--")
${FIND} $* -iname "*.${__searchWith}" -type f
;;
*)
local __searchToken="${__flag}"
${FIND} $* -name "*.${__searchWith}" -type f | ${GREP} -i "${__searchToken}"
;;
esac
return $?
}

Comments

Popular posts from this blog

Magical Places: Dragon's Triangle

Cruel History

Magical number :73