Wednesday, August 13, 2008

The Unix/Linux 'search' command

How to find files in Linux or Unix using 'find'


The 'find' command is very effective in searching for files in my Unix or Linux box. Here's how you do it:

# find <path> -name "<filename>" -print

Example 1: Let us search for the file 'sshd' from the root('/') directory.

# find / -name "sshd" -print
./usr/sbin/sshd
./usr/src/etc/pam.d/sshd
./usr/src/etc/rc.d/sshd
./usr/src/secure/usr.sbin/sshd
./etc/pam.d/sshd
./etc/rc.d/sshd
#

So, there it is, the 'sshd' file can be found from those six different locations.

Searching a File containing a particular text string in Unix or Linux


Again, I use the 'find' command for this. The syntax is as follow:

# find <path> -exec grep "<search_string>" '{}' \; -print

Example 2: Let us search for files that contains the string "dhclient_start" from the etc('/etc') directory.

# find /etc -exec grep "dhclient_start" '{}' \; -print
start_cmd="dhclient_start"
dhclient_start()
/etc/rc.d/dhclient
#

The output tells us that it has only found one file that contains the string we were looking for. It can be found inside the file '/etc/rc.d/dhclient'.

This is my simplified 'search' command


I tried to simplify the find command by creating a shell script which will accept only three parameters:

# search <search_path> <search_type> <search_string>

Where:
search_path:
ex. /usr/local
search_type:
file - search for a file/directory
content - search for a file content
search_string:
- the filename or the file content you want to search

Example 3: Let us search for the file 'sshd' from the root('/') directory.

# search / file "sshd"
./usr/sbin/sshd
./usr/src/etc/pam.d/sshd
./usr/src/etc/rc.d/sshd
./usr/src/secure/usr.sbin/sshd
./etc/pam.d/sshd
./etc/rc.d/sshd
#

The output is the same with Example 1 but as you notice we only have three parameters in our command which makes is simpler to understand.

Example 4: Let us search for files that contains the string "dhclient_start" from the etc('/etc') directory.

# search /etc content "dhclient_start"
start_cmd="dhclient_start"
dhclient_start()
/etc/rc.d/dhclient
#

The output is also the same with Example 2 and again the command only take three parameters.

Here is the script, you can copy and paste this script and save it to a file named 'search'. After saving the file, change the permission of the file to '+x' and copy it to '/usr/local/bin'.

#!/bin/sh
# Filename: search
# Description: A simplified find command
# Author: Obispo

searchPath=""
searchType=""
searchString=""
findcmd="/usr/bin/find"
scriptUsage="
Usage:
${0} <search_path> <search_type> <search_string>
search_path:
ex. /usr/local
search_type:
file - search for a file/directory
content - search for a file content
search_string:
- the filename or the file content you want to search
"

# Script Usage
display_usage()
{
echo "${scriptUsage}"
exit 1
}

if [ "${1}" = "" ]; then
display_usage
elif [ "${2}" = "" ]; then
display_usage
elif [ "${3}" = "" ]; then
display_usage
else
searchPath=${1}
searchType=${2}
searchString=${3}
fi

case "${searchType}" in
file)
${findcmd} ${searchPath} -name "${searchString}" -print
;;
content)
${findcmd} ${searchPath} -exec grep "${searchString}" '{}' \; -print
;;
*)
display_usage
;;
esac

exit 0


Enjoy!


No comments: