Sponsored Content
Top Forums Shell Programming and Scripting recursive searching for files in directory that matches a particular name - taking care of links Post 302413545 by methyl on Friday 16th of April 2010 05:17:03 AM
Old 04-16-2010
Hi vickylife. I tried your recursion approach but could not make it work for a tree of any size because my shell collapsed due due to nesting loops too deep.

Just for interest tried another approach to navigating the directory tree using just "ls" and Shell commands. The script looks at the start directory and then looks for directories. If it finds any directories it stores the list in a file. Next time round it reads the list from the previous iteration and repeats the process.
The script looks long but you will see that half the script is concerned with tidying up workfiles.
The output is a list of all the directories from the start directory down.

It is relatively trivial to adapt the script to take parameters and to record files of a particular name into an output file, but I'll leave that bit to you. If you are non-root you may need to test for read access to each directory.

I couldn't make out whether you wanted to follow links or not. If you do, the "ls" needs a "-L" switch.



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
pwd > ${WORKFILE1}
#
while true
do
        > ${WORKFILE2}
        cat ${WORKFILE1}|while read DIR
        do
                cd "${DIR}"
                pwd             # Output to screen
                # 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


Footnote: Also looked at "ls -laR" approach by felt that processing the output really needed "awk" which would defeat the object because we would then be tempted to do the whole process in "awk". The output format of "ls -1R" is similarly awkward to process.

Last edited by methyl; 04-16-2010 at 06:44 AM.. Reason: Footnote:
 

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
platform::shell(n)					       Tcl Bundled Packages						platform::shell(n)

__________________________________________________________________________________________________________________________________________________

NAME
platform::shell - System identification support code and utilities SYNOPSIS
package require platform::shell ?1.1.4? platform::shell::generic shell platform::shell::identify shell platform::shell::platform shell _________________________________________________________________ DESCRIPTION
The platform::shell package provides several utility commands useful for the identification of the architecture of a specific Tcl shell. This package allows the identification of the architecture of a specific Tcl shell different from the shell running the package. The only requirement is that the other shell (identified by its path), is actually executable on the current machine. While for most platform this means that the architecture of the interrogated shell is identical to the architecture of the running shell this is not generally true. A counter example are all platforms which have 32 and 64 bit variants and where a 64bit system is able to run 32bit code. For these running and interrogated shell may have different 32/64 bit settings and thus different identifiers. For applications like a code repository it is important to identify the architecture of the shell which will actually run the installed packages, versus the architecture of the shell running the repository software. COMMANDS
platform::shell::identify shell This command does the same identification as platform::identify, for the specified Tcl shell, in contrast to the running shell. platform::shell::generic shell This command does the same identification as platform::generic, for the specified Tcl shell, in contrast to the running shell. platform::shell::platform shell This command returns the contents of tcl_platform(platform) for the specified Tcl shell. KEYWORDS
operating system, cpu architecture, platform, architecture platform::shell 1.1.4 platform::shell(n)
All times are GMT -4. The time now is 05:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy