Showing posts with label shell script. Show all posts
Showing posts with label shell script. Show all posts

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!


Tuesday, May 6, 2008

Transferring Files using Shell Script

I have a directory where all my test codes are in there. I have test codes for C, shell scripts, perl and etc inside this directory. I realized that I could organize it further if I'll create a directory for c source codes, shell script source codes and etc. If I'll do the task manually, it will be a repetitive task. So I wrote a shell script to do the task for me.

#!/bin/sh
# Filename: organize.sh
# Description: A simple program that will transfer
# selected files to a certain directory
# Author: Obispo

# create a directory for c files
mkdir cfiles

# create a directory for sh files
mkdir shfiles

# transfer all c source codes to cfiles directory
for f in `ls *.c`
do
mv ${f} ./cfiles
done

# transfer all shell scripts to shfiles directory
for f in `ls *.sh`
do
mv ${f} ./shfiles
done

exit 0

Okay, here's how I came up with the program. If I'll execute the command 'ls *.c' at the console, it will display all the files that ends with '.c' right? So what I did was to get these files that ends with '.c' one by one and place it in a variable called 'f' then move it to my cfiles directory. The Unix command to move a file to another directory is 'mv'.


Friday, May 2, 2008

Counting Numbers in Shell Scripts

Scripts are very useful especially in automating repetitive task for system administration. Let me share to you a very simple shell script that will display numbers from 1 to 10 using while loop.

#!/bin/sh
# Filename: count.sh
# Description: Display numbers 1 to 10 using while loop
# Author: Obispo

# initialize a variable
i=1

# do the loop
while [ ${i} -le 10 ]
do
# you may place your repetitive task here
echo "count: ${i}"
i=`expr ${i} + 1`
done

exit 0


Output:
# ./count.sh
count: 1
count: 2
count: 3
count: 4
count: 5
count: 6
count: 7
count: 8
count: 9
count: 10


You're probably wondering whats the use of printing numbers 1 to 10 in the screen? I know how to that even when my eyes are closed. Well, all I can say is that you will need it someday when you grow up. :)