Sponsored Content
Top Forums Shell Programming and Scripting recursive searching for files in directory that matches a particular name - taking care of links Post 302413789 by vickylife on Saturday 17th of April 2010 07:01:50 AM
Old 04-17-2010
Hi Methyl,

Thank you for your quick help. I have adopted your code and make subtle changes to suit my purpose. this is how the code currently looks like :
Code:
#!/bin/ksh
PN="`basename $0`"
#
WORKFILE1=/var/tmp/${PN}.wf1.$$
WORKFILE2=/var/tmp/${PN}.wf2.$$
#

MYEXIT ()
{
if [ -f "${WORKFILE1}" ]
then
        rm "${WORKFILE1}"
fi
if [ -f "${WORKFILE2}" ]
then
        rm "${WORKFILE2}"
fi
#
exit
}
#
trap 'MYEXIT' 1 2 3 15
#
# Seed process with current directory
if [ $# -ne 2 ]; 
	then echo "Please run the script as $0 NameOfFile PathToDirectory" 
	exit 1 
fi 
FILE=$1 
DIRECTORY=$2


echo $DIRECTORY > ${WORKFILE1}
#
while true
do
        > ${WORKFILE2}
        cat ${WORKFILE1}|while read DIR
        do
                cd "${DIR}"
                if [ -f $FILE ]; then
			thisPath=`pwd`
			echo "$thisPath/$FILE"   # Output to screen
		fi
                # Find any subdirectories. Allow for none
                ls -1d * 2>/dev/null | while read DIR2
                do
                        if [ -d "${DIR2}" ]
                        then
                                cd "${DIR2}"
                                pwd >> ${WORKFILE2}
                                cd ..
                        fi
                done
        done
        # If no more subdirectories we have finished
        if [ -s ${WORKFILE2} ]
        then
                # Seed next iteration
                cp -p ${WORKFILE2} ${WORKFILE1}
        else
                break
        fi
done
#
MYEXIT

Now my question is related to links. I need to traverse through links too provided they do not loop back and form an infinite loop. How do i do that??
 

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching files in a directory.Urgent

Hi everyone, I need to search files starting with RPT_0, RPT_1,........ in a directory. How can I implement that through a shell script. Also I want to read the last line of each file after searching them. Can someone help me out in this regard. One more thing how I can extract a particular... (7 Replies)
Discussion started by: srivsn
7 Replies

2. Shell Programming and Scripting

Searching for files over 30 days old in current directory

May be a simple question for experts here.... I need to get the list of files older than 30 days in the current folder. I tried "find", but it searches recursively in all the sub directories. Can I restrict the recursive search and extract the files only from current directory ? (18 Replies)
Discussion started by: cxredd4
18 Replies

3. UNIX for Advanced & Expert Users

rsync: taking advantage of files in different directory other than destination

Dear Folks, I have to backup pgsql database dump everynight on a routine. The database dump actually contains sql(text) statements. The actual size of the database dump is aroung 800 MB. Between two days backup, only few lines of statements are modified/added/deleted. I dont want to do... (1 Reply)
Discussion started by: rssrik
1 Replies

4. UNIX for Dummies Questions & Answers

searching files inside directory

hey, i need to use grep to search a bunch of header files inside a directory to return which file i can find the function i'm searching for in. how do i use wild cards to search through the files? i can only figure out how to search inside the directory, not inside the files that are in the... (4 Replies)
Discussion started by: kylethesir
4 Replies

5. Solaris

Searching for files in a Directory

Hi, I am trying to write a script that will search in a particular directory and tell me how many files are in there. I then want to be able to email certain users of how many files are in that directory and what the file names are? any help would be great as i am getting confused. thanks (3 Replies)
Discussion started by: Pablo_beezo
3 Replies

6. Shell Programming and Scripting

Help needed with searching files and moving them to a different directory

I am new to shell scripting. Can someone help me out with this one please? I need to write a script fot the following scenario: I am currently in /parent directory. I have a set of files in /parent/error_files directory My script has to search for a file in /parent/erratic_files... (1 Reply)
Discussion started by: ss3944
1 Replies

7. Shell Programming and Scripting

Count number of pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies
PWD(1)							    BSD General Commands Manual 						    PWD(1)

NAME
pwd -- return working directory name SYNOPSIS
pwd [-LP] DESCRIPTION
The pwd utility writes the absolute pathname of the current working directory to the standard output. Some shells may provide a builtin pwd command which is similar or identical to this utility. Consult the builtin(1) manual page. The options are as follows: -L Display the logical current working directory. -P Display the physical current working directory (all symbolic links resolved). If no options are specified, the -P option is assumed. ENVIRONMENT
Environment variables used by pwd: PWD Logical current working directory. DIAGNOSTICS
The pwd utility exits 0 on success, and >0 if an error occurs. STANDARDS
The pwd utility conforms to IEEE Std 1003.1-2001 (``POSIX.1''). SEE ALSO
builtin(1), cd(1), csh(1), sh(1), getcwd(3) BUGS
In csh(1) the command dirs is always faster because it is built into that shell. However, it can give a different answer in the rare case that the current directory or a containing directory was moved after the shell descended into it. The -L option does not work unless the PWD environment variable is exported by the shell. BSD
February 4, 2002 BSD
All times are GMT -4. The time now is 01:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy